5月27日学习日志

今天学习了SmsManager(短信管理器)。

核心代码:

public void sendSMS(String phoneNumber,String message){  
    //获取短信管理器   
    android.telephony.SmsManager smsManager = android.telephony.SmsManager.getDefault();  
    //拆分短信内容(手机短信长度限制),貌似长度限制为140个字符,就是
    //只能发送70个汉字,多了要拆分成多条短信发送
    //第四五个参数,如果没有需要监听发送状态与接收状态的话可以写null    
    List<String> divideContents = smsManager.divideMessage(message);   
    for (String text : divideContents) {    
        smsManager.sendTextMessage(phoneNumber, null, text, sentPI, deliverPI);    
    }  
} 
//处理返回的发送状态   
String SENT_SMS_ACTION = "SENT_SMS_ACTION";  
Intent sentIntent = new Intent(SENT_SMS_ACTION);  
PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, sentIntent,  0);  
//注册发送信息的广播接收者
context.registerReceiver(new BroadcastReceiver() {  
    @Override  
    public void onReceive(Context _context, Intent _intent) {  
        switch (getResultCode()) {  
        case Activity.RESULT_OK:
            Toast.makeText(context, "短信发送成功", Toast.LENGTH_SHORT).show();  
            break;  
        case SmsManager.RESULT_ERROR_GENERIC_FAILURE:    //普通错误
            break;
        case SmsManager.RESULT_ERROR_RADIO_OFF:         //无线广播被明确地关闭
            break;          
        case SmsManager.RESULT_ERROR_NULL_PDU:          //没有提供pdu
            break;      
        case SmsManager.RESULT_ERROR_NO_SERVICE:         //服务当前不可用
            break;              
        }  
    }  
}, new IntentFilter(SENT_SMS_ACTION)); 
//处理返回的接收状态   
String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";  
//创建接收返回的接收状态的Intent  
Intent deliverIntent = new Intent(DELIVERED_SMS_ACTION);  
PendingIntent deliverPI = PendingIntent.getBroadcast(context, 0,deliverIntent, 0);  
context.registerReceiver(new BroadcastReceiver() {  
   @Override  
   public void onReceive(Context _context, Intent _intent) {  
       Toast.makeText(context,"收信人已经成功接收", Toast.LENGTH_SHORT).show();  
   }  
}, new IntentFilter(DELIVERED_SMS_ACTION)); 
原文地址:https://www.cnblogs.com/20193925zxt/p/14910707.html