Pro Android学习笔记(一一九):Telephony API(1):发送短信

文章转载只能用于非商业性质,且不能带有虚拟货币、积分、注册等附加条件。转载须注明出处http://blog.csdn.net/flowingflying以及作者@恺风Wei。

在Android,接口封装得很好,发送短信很容易实现,

小例子代码

Pro Android学习笔记(一一九):Telephony API(1):发送短信_第1张图片 public class SendSMSActivity extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.send_sms_activity);       
    }

   
    public void doSend(View v){
        EditText phoneET = (EditText)findViewById(R.id.calledNumber);
        EditText smsET = (EditText)findViewById(R.id.sms);         
        sendSMS(phoneET.getText().toString(),smsET.getText().toString());
    }

   
    private void sendSMS(String called, String msg){
        try{
            // SmsManager位于android.telephony包
            SmsManager smsMgr = SmsManager.getDefault(); 
            //sendTextMessage()的参数1 :String destinationAddressString destinationAddress 是对方号码,
            // 参数2:String smscAddress短信中心,在以前早期的功能手机中,会有短信中心设置,Android还一下没找到在哪,短信中心的号码和运营商及手机归属地有关,OEM在手机出厂的时候会设置号,用null,表示用手机现有的设置
           // 参数3:短信内容
           // 参数4:PendingIntent sentIntent,短信发送触发的Intent
           // 参数5:Pending deliveryIntent,对方接受短信触发的Intent。
            smsMgr.sendTextMessage(called, null, msg, null, null);
            Toast.makeText(this, "Send SMS", Toast.LENGTH_SHORT).show();
        }catch(Exception e){
            Toast.makeText(this, "Fail to Send Msg!", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    }

}

发送短信是需要授权的,在AndroidManifest.xml中加入:

<uses-permission android:name="android.permission.SEND_SMS" />

获得回执

小例子中最为关键的就是sendTextMessage(),如果我们希望有发送以及接受的回执,我们可以利用第4个和第5个参数。

private void sendSMS(String called, String msg){
    try{
        SmsManager smsMgr = SmsManager.getDefault();

       
        Intent intent1 = new Intent(MyReceiver.SMS_ALERT);
        intent1.putExtra("phone", called);
        intent1.putExtra("message", "send");
        PendingIntent pi1 = PendingIntent.getBroadcast(this, 1000, intent1, 0);
       
        Intent intent2 = new Intent(MyReceiver.SMS_ALERT);
        intent2.putExtra("phone", called);
        intent2.putExtra("message", "received");
        PendingIntent pi2 = PendingIntent.getBroadcast(this, 1001, intent2, 0);
                   
        smsMgr.sendTextMessage(called, null, msg, pi1, pi2);
    }catch(Exception e){
        Toast.makeText(this, "Fail to Send Msg!", Toast.LENGTH_SHORT).show();
        e.printStackTrace();
    }

}

接收器简单地弹toast:

public class MyReceiver extends BroadcastReceiver{
    public final static String SMS_ALERT = "cn.wei.flowingflying.testtelephony.SMS_ALERT";
    @Override
    public void onReceive(Context context, Intent intent) {   
        Bundle extras = intent.getExtras();
        if(extras != null){
            String message = extras.getString("message");
            String phone = extras.getString("phone");
           
            if(message.contentEquals("send")){
                int result = getResultCode();   //如果成功result code为Activity_RESULT_OK,失败为RESULT_ERROR_XXXX,具体看reference。
                Toast.makeText(context, "Send message to " + phone +
                        (result == Activity.RESULT_OK ? " successfully":" failed") + "!",
                        Toast.LENGTH_SHORT).show();                
            }else if(message.contentEquals("received")){
                Toast.makeText(context, phone + " received message!", Toast.LENGTH_SHORT).show();
            }
        }
    }
}

发送长短信

如果,我们在EditText中输入很长的信息,超过了短信的长度,仍然直接使用sendTextMessage()进行发送,会出现异常。在我的实体机测试例子中,还导致系统重启。我们需要进行短信分拆,同样地如果我们需要收发的回执,也需要多个PendingIntent,下面是代码片段:

private void sendSMS2(String called,String msg){
    try{
        SmsManager smsMgr = SmsManager.getDefault();
        //1、进行短信分拆
       ArrayList<String> subMsg = smsMgr.divideMessage(msg);
        if(subMsg.size() > 1){               
            ArrayList<PendingIntent> piArraySend = new ArrayList<PendingIntent>();
            ArrayList<PendingIntent> piArrayRecv = new ArrayList<PendingIntent>();
            for(int i = 0; i <subMsg.size();i ++){ 
                piArraySend.add(getSendPendingIntent(called,1000));
                piArrayRecv.add(getRecvPendingIntent(called,2000));
            }               
           //2、发送长短信
            smsMgr.sendMultipartTextMessage(called, null, subMsg, piArraySend, piArrayRecv);

        }else{
            smsMgr.sendTextMessage(called, null, msg,
                    getSendPendingIntent(called,1000),
                    getRecvPendingIntent(called,2000));

        }

    }catch(Exception e){
        Toast.makeText(this, "Fail to Send Msg!", Toast.LENGTH_SHORT).show();
        e.printStackTrace();
    }

}

private PendingIntent getSendPendingIntent(String phone, int code){
    Intent intent = new Intent(MyReceiver.SMS_ALERT);
    intent.putExtra("phone", phone);
    intent.putExtra("message", "send");
    return PendingIntent.getBroadcast(this, code, intent, 0);
}

private PendingIntent getRecvPendingIntent(String phone, int code){
    Intent intent = new Intent(MyReceiver.SMS_ALERT);
    intent.putExtra("phone", phone);
    intent.putExtra("message", "received");
    return PendingIntent.getBroadcast(this, code, intent, 0);
}

例如,发送一段很长的文字,需要分拆为3个短信,将收到3次短信发送成功的回执。但是现在长短信可以只能手机中合成,只收到1条短信接收成功的回执,这和具体的运营商有关。

小例子代码在:Pro Android学习:telephony小例子

相关链接:我的Android开发相关文章

你可能感兴趣的:(Pro Android学习笔记(一一九):Telephony API(1):发送短信)