Android实战技巧之三十九:短信收发

7月4日从广州出差回来就定下写作计划,但迟迟没有动笔。耽搁的原因还是老样子,工作上又有新任务,全部精力都投入过去了,每天精疲力竭的回来也打不起精神做其他事了。这就是精力管理不当所致,就像我把很多要做的事无情的放到“等有时间”再做一样。今晚,我一定要给自己一个交待。不论文章写的如何,但不动笔就永远是零。

正文在下面

一直以来,Android的手机功能(通话与短信)都放在android.telephony包中,到了4.4时(也就是API19)android.provider.Telephony及相关类横空出世辅助电话功能以及制定安卓世界手机功能的新秩序。 我们的短信功能用到了SmsManager和SmsMessage两个主要类。

发送短信via Intent

万能的intent能够帮我们做很多事,只要你有“意图”它就会满足你。

private void sendMessageViaSystem() {Uri uri = Uri.parse(“smsto:”+etNumber.getText());Intent intent = new Intent(Intent.ACTION_VIEW,uri);intent.putExtra(“sms_body”,etMessage.getText().toString());//intent.setType(“rnd”);startActivity(intent);}发送短信via SmsManager () {SmsManager smsManager = SmsManager.getDefault();smsManager.sendTextMessage(etNumber.getText().toString(), null,etMessage.getText().toString(), null, null);}

最简单的发送短信条件就是有电话号码和短信内容,调用SmsManager的sendTextMessage方法即可。

监听短信的发送状态

sendTextMessage方法的后两个参数是PendingIntent,函数原型如下:

* @this <code>PendingIntent</code> is* broadcast when the message is successfully sent, or failed.* The result code will be <code>Activity.RESULT_OK</code> for success,* these errors:<br>* <code>RESULT_ERROR_GENERIC_FAILURE</code><br>* <code>RESULT_ERROR_RADIO_OFF</code><br>* <code>RESULT_ERROR_NULL_PDU</code><br>* For <code>RESULT_ERROR_GENERIC_FAILURE</code> the sentIntent may include* the extra “errorCode” containing a radio technology specific value,* generally only useful for troubleshooting.<br>* The per-application based SMS control checks sentIntent. If sentIntent* is NULL the caller will be checked against all unknown applications,* which cause smaller number of SMS to be sent in checking period.* @this <code>PendingIntent</code> is* broadcast when the message is delivered to the recipient. The* raw pdu extended data (“pdu”).public void sendTextMessage(String destinationAddress, String scAddress, String text,PendingIntent sentIntent, PendingIntent deliveryIntent)

而我们要监听发送状态就要用到这两个参数。与这个两个PendingIntent联系的是两个Broadcast Receiver,其会接收到发送过程中状态广播。就像上面参数解释的一样。 代码示例如下:

private String SMS_SEND_ACTIOIN = “SMS_SEND”;private String SMS_DELIVERED_ACTION = “SMS_DELIVERED”;private SmsStatusReceiver mSmsStatusReceiver;private SmsDeliveryStatusReceiver mSmsDeliveryStatusReceiver;() {super.onResume();mSmsStatusReceiver = new SmsStatusReceiver();registerReceiver(mSmsStatusReceiver,new IntentFilter(SMS_SEND_ACTIOIN));mSmsDeliveryStatusReceiver = new SmsDeliveryStatusReceiver();registerReceiver(mSmsDeliveryStatusReceiver,new IntentFilter(SMS_DELIVERED_ACTION));}() {super.onPause();unregisterReceiver(mSmsStatusReceiver);unregisterReceiver(mSmsDeliveryStatusReceiver);}() {SmsManager smsManager = SmsManager.getDefault();PendingIntent sentIntent = PendingIntent.getBroadcast(this, 0, new Intent(SMS_SEND_ACTIOIN), 0);PendingIntent deliveryIntent = PendingIntent.getBroadcast(this, 0,new Intent(SMS_DELIVERED_ACTION), 0);smsManager.sendTextMessage(etNumber.getText().toString(), null,etMessage.getText().toString(), sentIntent, deliveryIntent);Log.d(TAG,”sent message.”);}{(Context context, Intent intent) {Log.d(TAG,”SmsStatusReceiver onReceive.”);switch(getResultCode()) {case Activity.RESULT_OK:Log.d(TAG, “Activity.RESULT_OK”);break;case SmsManager.RESULT_ERROR_GENERIC_FAILURE:Log.d(TAG, “RESULT_ERROR_GENERIC_FAILURE”);break;case SmsManager.RESULT_ERROR_NO_SERVICE:Log.d(TAG, “RESULT_ERROR_NO_SERVICE”);break;case SmsManager.RESULT_ERROR_NULL_PDU:Log.d(TAG, “RESULT_ERROR_NULL_PDU”);break;case SmsManager.RESULT_ERROR_RADIO_OFF:Log.d(TAG, “RESULT_ERROR_RADIO_OFF”);break;}}}{(Context context, Intent intent) {Log.d(TAG,”SmsDeliveryStatusReceiver onReceive.”);switch(getResultCode()) {case Activity.RESULT_OK:Log.i(TAG, “RESULT_OK”);break;case Activity.RESULT_CANCELED:Log.i(TAG, “RESULT_CANCELED”);break;}}}短信接收

与发送状态监听类似,短信的接收也是用广播接收器。为了一直对短信广播的接收,我采用了在Manifest中注册广播的方法。

==>你怎么样对待别人被人就怎么样对待你。

Android实战技巧之三十九:短信收发

相关文章:

你感兴趣的文章:

标签云: