发送短信以后,我们所关心的就是短信有没有发送成功。Android中通过SmsManager可以实现监听短信发送状态的功能。
首先在MainActivity中声明:
//显示短信发送的状态
private TextView status;
//电话号码
private EditText phone;
//短信内容
private EditText content;
//发送行为
private Button sendButton;
//两个myServiceReceiver类成员变量
private myServiceReceiver mReceiver01,mReceiver02;
//自定义ACTION常数 作为广播的IntentFilter识别常数
private String SMS_SEND_ACTION = "SMS_SEND_ACTION";
private String SMS_DELIVERED_ACTION = "SMS_DELIVERED_ACTION";
接着实现onCreate方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
status = (TextView)findViewById(R.id.textview01);
phone = (EditText)findViewById(R.id.phone);
content = (EditText)findViewById(R.id.smscontent);
sendButton = (Button)findViewById(R.id.send);
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//获取手机号码
String phone_send = phone.getText().toString().trim();
//获取短信内容
String content_send = content.getText().toString().trim();
//创建SmsManager对象 来发送短信
SmsManager smsManager = SmsManager.getDefault();
try {
//创建自定义action常数的Intent 给(PendingIntent)参数之用
Intent itSend = new Intent(SMS_SEND_ACTION);
Intent itDeliver = new Intent(SMS_DELIVERED_ACTION);
//sendIntent参数为传送后接受的广播信息PendingIntent
PendingIntent sendPI = PendingIntent.getBroadcast(MainActivity.this,0,itSend,0);
//deliveryIntent参数为传送后接受的广播信息PendingIntent
PendingIntent deliverPI = PendingIntent.getBroadcast(MainActivity.this,0,itDeliver,0);
//发送短信 注意倒数的两个PendingIntent参数
smsManager.sendTextMessage(phone_send,null,content_send,sendPI,deliverPI);
status.setText("短信正在发送中...");
}catch (Exception e){
status.setText(e.toString());
e.printStackTrace();
}
}
});
}
自定义myServiceReceiver用户覆盖掉BroadcastReceiver聆听短信的功能 根据不同的发送状态来对应显示
public class myServiceReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
try {
//android.content.BroadcastReceiver.getResultCode()方法
// public static final int RESULT_ERROR_GENERIC_FAILURE = 1;
// public static final int RESULT_ERROR_NO_SERVICE = 4;
// public static final int RESULT_ERROR_NULL_PDU = 3;
// public static final int RESULT_ERROR_RADIO_OFF = 2;
switch (getResultCode()){
case Activity.RESULT_OK:
status.setText("短信发送成功");
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE://普通错误
status.setText("短信发送失败");
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
status.setText("服务当前不可用");
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
status.setText("没有提供pdu");
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
status.setText("无线广播被明确地关闭");
break;
}
}catch (Exception e){
status.setText(e.toString());
e.printStackTrace();
}
}
}
在onResume()方法和onPause()中作如下处理
protected void onResume() {
//自定义IntentFilter为SMS_SEND_ACTION Receiver
IntentFilter intentFilter;
intentFilter = new IntentFilter(SMS_SEND_ACTION);
mReceiver01 = new myServiceReceiver();
registerReceiver(mReceiver01,intentFilter);
//自定义IntentFilter为SMS_DELIVERED_ACTION Receiver
intentFilter = new IntentFilter(SMS_DELIVERED_ACTION);
mReceiver02 = new myServiceReceiver();
registerReceiver(mReceiver02,intentFilter);
super.onResume();
}
@Override
protected void onPause() {
// 取消注册自定义 Receiver
unregisterReceiver(mReceiver01);
unregisterReceiver(mReceiver02);
super.onPause();
}
网友评论