本章节翻译自《Beginning-Android-4-Application-Development》,如有翻译不当的地方,敬请指出。

原书购买地址http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/


上一节介绍了如何发送短信,但是如何知道短信被成功地发送了?这就需要创建两个PendingIntent对象去监视整个发送过程的状态。这两个PendingIntent对象作为参数传递给sendTextMessage()方法。

public class SMSActivity extends Activity {String SENT = "SMS_SENT";String DELIVERED = "SMS_DELIVERED";PendingIntent sentPI, deliveredPI;BroadcastReceiver smsSentReceiver, smsDeliveredReceiver;    IntentFilter intentFilter;        private BroadcastReceiver intentReceiver = new BroadcastReceiver() {        @Override        public void onReceive(Context context, Intent intent) {            //?display the SMS received in the TextView?            TextView SMSes = (TextView) findViewById(R.id.textView1);            SMSes.setText(intent.getExtras().getString("sms"));        }    };/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);sentPI = PendingIntent.getBroadcast(this, 0,new Intent(SENT), 0);deliveredPI = PendingIntent.getBroadcast(this, 0,new Intent(DELIVERED), 0);}@Overridepublic void onResume() {super.onResume();//---create the BroadcastReceiver when the SMS is sent---smsSentReceiver = new BroadcastReceiver(){@Overridepublic void onReceive(Context arg0, Intent arg1) {switch (getResultCode()){case Activity.RESULT_OK:Toast.makeText(getBaseContext(), "SMS sent",Toast.LENGTH_SHORT).show();break;case SmsManager.RESULT_ERROR_GENERIC_FAILURE:Toast.makeText(getBaseContext(), "Generic failure",Toast.LENGTH_SHORT).show();break;case SmsManager.RESULT_ERROR_NO_SERVICE:Toast.makeText(getBaseContext(), "No service",Toast.LENGTH_SHORT).show();break;case SmsManager.RESULT_ERROR_NULL_PDU:Toast.makeText(getBaseContext(), "Null PDU",Toast.LENGTH_SHORT).show();break;case SmsManager.RESULT_ERROR_RADIO_OFF:Toast.makeText(getBaseContext(), "Radio off",Toast.LENGTH_SHORT).show();break;}}};//---create the BroadcastReceiver when the SMS is delivered---smsDeliveredReceiver = new BroadcastReceiver(){@Overridepublic void onReceive(Context arg0, Intent arg1) {switch (getResultCode()){case Activity.RESULT_OK:Toast.makeText(getBaseContext(), "SMS delivered",Toast.LENGTH_SHORT).show();break;case Activity.RESULT_CANCELED:Toast.makeText(getBaseContext(), "SMS not delivered",Toast.LENGTH_SHORT).show();break;}}}; //---register the two BroadcastReceivers---registerReceiver(smsDeliveredReceiver, new IntentFilter(DELIVERED));      registerReceiver(smsSentReceiver, new IntentFilter(SENT));}@Overridepublic void onPause() {super.onPause();//---unregister the two BroadcastReceivers---unregisterReceiver(smsSentReceiver);unregisterReceiver(smsDeliveredReceiver);    }public void onClick(View v) {sendSMS("130xxxxxxxx", "Hello my friends!");}//?sends an SMS message to another device?private void sendSMS(String phoneNumber, String message){SmsManager sms = SmsManager.getDefault();sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);}}

首先,在onCreate里面初始化两个PendingIntent对象:

sentPI = PendingIntent.getBroadcast(this, 0,new Intent(SENT), 0);deliveredPI = PendingIntent.getBroadcast(this, 0,new Intent(DELIVERED), 0);

然后,在onResume里面注册相应的广播接收器:

registerReceiver(smsDeliveredReceiver, new IntentFilter(DELIVERED));      registerReceiver(smsSentReceiver, new IntentFilter(SENT));

接下来,传递两个PendingIntent实例:

SmsManager sms = SmsManager.getDefault();sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);

最后,在onPause里面卸载相应的广播接收器:

unregisterReceiver(smsSentReceiver);unregisterReceiver(smsDeliveredReceiver);    


更多相关文章

  1. android客户端连接人人网之二----获取朋友信息
  2. 图片处理
  3. 屏幕元素的层次
  4. Android(安卓)内存优化
  5. android Handler消息处理源码剖析
  6. Android传感器之陀螺仪传感器
  7. Volley+Gson
  8. Handler的理解
  9. Android传感器之磁场传感器

随机推荐

  1. android Java BASE64编码和解码二:图片的
  2. ubuntu 下android1.5 尝试
  3. BroadcastReceiver实现android来去电录音
  4. Android中几种图像特效处理方法小结
  5. Android相关文档资源大放送 感兴趣的话可
  6. Android开发者指南(12) ―― Android(安
  7. 关于设置activity样式
  8. android 的内存、内部存储和外部存储的理
  9. android TabHost(选项卡)的使用方法
  10. Android(安卓)Service dump使用