Android创建简单发送和接收短信应用

2019-12-10 18:14:02刘景俊

3、 声明权限:

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

二、发送短信

1、 发送短信:用getDefault方法得到SmsManager对象,调用sendTextMessage方法发送短信

// 发送短信
public void send(View view) {
  SmsManager smsManager = SmsManager.getDefault();
  String destinationAddress = "15527100207";
  String text = "我爱你!";

  Intent intent1 = new Intent(SENT_SMS_ACTION);
  PendingIntent sentIntent = PendingIntent.getBroadcast(this, 0, intent1, 0);
  Intent intent2 = new Intent(DELIVERY_SMS_ACTION);
  PendingIntent deliveryIntent = PendingIntent.getBroadcast(this, 0, intent2, 0);
  smsManager.sendTextMessage(destinationAddress, null, text, sentIntent, deliveryIntent); // aidl服务,进程间的通信
}

 

 我们具体了解一下sendTextMessage方法一个参数的含义:

public void sendTextMessage(String destinationAddress, String scAddress, String text,PendingIntent sentIntent, PendingIntent deliveryIntent)
destinationAddress: // 目的地,也就是对方的手机号
scAddress:     // 服务中心地址,为空的话就是默认的SMSC
text:        // 发送消息的具体内容
sentIntent:     // 当消息成功或者失败发送时,就发起这个广播
deliveryIntent:   // 当消息到达目的地时,就发起这个广播

 2、 sentIntent的广播: