监听短信接收者笔记

来源:互联网 发布:全球中文地图导航软件 编辑:程序博客网 时间:2024/05/16 17:38

第一步:在清单文件中注释receiver name指定要继承的broadcastReceiver <intent-filter priority设定短信接收的优先级><action name指定sms_received记得加primission权限read_sms与receive_sms

第二步:新建一个类继承boardcasereceiver,在onreceive方法当中   


通过 Object[] obj = (Object[])intent.getExtra().get("pdus");拿到一组短信流

for(int i = 0 ; i < obj.length; i++){

     SmsMessage sm = SmsMessage.createFromPdu((byte[])obj[i]);//注意 smsmessage别引入gsm包下的

     //下面就可以拿到短信的originalAddress与messagebody了

   //通过abortBroadcastReceiver结束掉这个广播了


//下面是发送短信,也在这里写一下吧

/**

*smsmanager sms = smsmanager.getdefault();//拿到实例,记得别引入gsm的

*sms.sendtextmessage();//以下为参数说明

Parameters:
destinationAddress the address to send the message to
scAddress is the service center address or null to use the current default SMSC
text the body of the message to send
sentIntent if not NULL this PendingIntent is broadcast when the message is successfully sent, or failed. The result code will beActivity.RESULT_OK for success, or one of these errors:
RESULT_ERROR_GENERIC_FAILURE
RESULT_ERROR_RADIO_OFF
RESULT_ERROR_NULL_PDU
For RESULT_ERROR_GENERIC_FAILURE the sentIntent may include the extra "errorCode" containing a radio technology specific value, generally only useful for troubleshooting.
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.
deliveryIntent if not NULL this PendingIntent is broadcast when the message is delivered to the recipient. The raw pdu of the status report is in the extended data ("pdu").

*/

}



+++++++++++++++++++++++++++++++++++++++++++++++

自定义广播

1需要在清单文件当中设置receiver,name指定继承boardcastreceiver的类了 这个没什么好说的

2<intent-filter><action name="com.tiger.xx"></action></intent-filter>//这里不需要指定优先级,action中的name是自定义的

3在activity当中通过按钮来发送一个有序广播 Intent intent = new Intent();     intent.setAction("com.tiger.xx");   sendOrderBroadcastReceiver();

以下是参数说明

Parameters:
intent The Intent to broadcast; all receivers matching this Intent will receive the broadcast.
receiverPermission String naming a permissions that a receiver must hold in order to receive your broadcast. If null, no permission is required.
resultReceiver Your own BroadcastReceiver to treat as the final receiver of the broadcast.
scheduler A custom Handler with which to schedule the resultReceiver callback; if null it will be scheduled in the Context's main thread.
initialCode An initial value for the result code. Often Activity.RESULT_OK.
initialData An initial value for the result data. Often null.
initialExtras An initial value for the result extras. Often null.

0 0
原创粉丝点击