【Android 开发教程】获取发送短信后的反馈
2013-09-03 10:15
621 查看
本章节翻译自《Beginning-Android-4-Application-Development》,如有翻译不当的地方,敬请指出。
原书购买地址http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/
上一节介绍了如何发送短信,但是如何知道短信被成功地发送了?这就需要创建两个PendingIntent对象去监视整个发送过程的状态。这两个PendingIntent对象作为参数传递给sendTextMessage()方法。
首先,在onCreate里面初始化两个PendingIntent对象:
然后,在onResume里面注册相应的广播***:
接下来,传递两个PendingIntent实例:
最后,在onPause里面卸载相应的广播***:
原书购买地址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. */ @Override public 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); } @Override public void onResume() { super.onResume(); //---create the BroadcastReceiver when the SMS is sent--- smsSentReceiver = new BroadcastReceiver(){ @Override public 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(){ @Override public 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)); } @Override public 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);
相关文章推荐
- 【Android 开发教程】通过编码实现发送短信
- 【Android 开发教程】使用Intent发送短信
- Android基础教程之----SMS简单发送短信程序(两个模拟器之间的通信)!
- Android开发之监听或获取手机短信内容
- Android开发之调用系统的ContentProvider——短信的获取和备份
- ios开发:获取手机通讯录、发送短信
- Android开发--电话拨号、短信发送
- Android开发之发送短信
- android开发教程之获取power_profile.xml文件的方法(android运行时能耗值)
- Android软件开发之发送短信与系统短信库解析
- Android软件开发之发送短信与系统短信库解析
- Android开发案例发送短信调用SmsManager 03
- Android 获取SIM卡信息,以及短信的发送和群发
- Android开发获取短信的内容并截取短信
- Android开发之监听或获取手机短信内容
- Android开发入门之发送短信
- 【Android 开发教程】编码实现获取与修改预设信息中的值
- Android开发之Android5.1.1(CM12.1)源码中短信发送流程解析
- Android开发之发送短信程序
- Android开发之Contentprovider实现获取手机短信((收)发件人、短信内容、收(发)件时间)