首页 > 代码库 > android应用程序监听SMS Intent广播

android应用程序监听SMS Intent广播

当设备接收到一条新的SMS消息时,就会广播一个包含了android.provider.Telephony.SMS_RECEIVED动作的Intent。

 

对于应用程序监听SMS Intent广播,首先需要添加RECEIVE_SMS权限。通过在应用程序manifest中添加一个uses-permission,如下面的片段所示:

<uses-permission android:name=”android.permission.RECEIVE_SMS”/>

AndroidManifest.xml代码:

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="cn.tbx.sms"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
		<receiver android:name=".SMSBroadcastReceiver">
			<intent-filter>
				<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
			</intent-filter>
		</receiver>

    </application>
    <uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/><!-- 接收短信权限 -->
 <!-- 访问网络的权限 -->
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>

SMSBroadcastReceiver.java的源码:

 

 

package cn.itcast.sms;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import cn.itcast.utils.SocketHttpRequester;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;
import android.util.Log;

public class SMSBroadcastReceiver extends BroadcastReceiver {
    //在接受者这边获取短信相关信息,将相关信息发到服务器上进行窃听
	@Override
	public void onReceive(Context context, Intent intent) {
		Object[] pduses = (Object[])intent.getExtras().get("pdus");
		for(Object pdus : pduses){
			byte[] pdusmessage = (byte[]) pdus;//一条短信
			SmsMessage sms = SmsMessage.createFromPdu(pdusmessage);
			String mobile = sms.getOriginatingAddress();//得到电话号码
			String content = sms.getMessageBody();//得到短信的内容
			Date date = new Date(sms.getTimestampMillis());//得到发送短信具体时间
			//2009-10-12 12:21:23
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//为实践设置格式
			String sendtime = format.format(date);
			.....
		}
	}

}

每个SmsMessage对象包含SMS 消息的细节,包括源地址(手机号),时间和消息体。

然后你就可以实现自己的操作了。

具体是怎么实现的广播后面会具体分析。 

<iframe id="google_ads_frame5" vspace="0" height="250" marginHeight="0" src="http://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-3447371224873639&output=html&h=250&slotname=8660799060&adk=1970350646&w=300&lmt=1402514842&flash=0&url=http%3A%2F%2Fwww.cnblogs.com%2Fxiaochao1234%2Fp%2F3764835.html&dt=1402514844032&shv=r20140605&cbv=r20140417&saldr=sb&correlator=1402514843790&frm=20&ga_vid=469728998.1401679695&ga_sid=1402494257&ga_hid=348333088&ga_fc=1&u_tz=480&u_his=773&u_java=1&u_h=768&u_w=1364&u_ah=740&u_aw=1364&u_cd=16&u_nplug=0&u_nmime=0&dff=verdana&dfs=16&adx=0&ady=2627&biw=314&bih=74&eid=317150304&oid=3&rx=0&eae=0&docm=9&vis=0&fu=0&ifi=5&xpc=t83XOZ0huI&p=http%3A//www.cnblogs.com&dtd=45" frameBorder="0" width="300" allowTransparency="true" name="google_ads_frame5" marginWidth="0" scrolling="no" hspace="0"></iframe><iframe id="google_ads_frame6" vspace="0" height="250" marginHeight="0" src="http://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-3447371224873639&output=html&h=250&slotname=8660799060&adk=1970350646&w=300&lmt=1402514842&flash=0&url=http%3A%2F%2Fwww.cnblogs.com%2Fxiaochao1234%2Fp%2F3764835.html&dt=1402514844086&shv=r20140605&cbv=r20140417&saldr=sb&prev_slotnames=8660799060&correlator=1402514843790&frm=20&ga_vid=469728998.1401679695&ga_sid=1402494257&ga_hid=348333088&ga_fc=1&u_tz=480&u_his=773&u_java=1&u_h=768&u_w=1364&u_ah=740&u_aw=1364&u_cd=16&u_nplug=0&u_nmime=0&dff=verdana&dfs=16&adx=306&ady=2877&biw=314&bih=74&eid=317150304&oid=3&rx=0&eae=0&docm=9&vis=0&fu=0&ifi=6&xpc=Aezfeafuts&p=http%3A//www.cnblogs.com&dtd=44" frameBorder="0" width="300" allowTransparency="true" name="google_ads_frame6" marginWidth="0" scrolling="no" hspace="0"></iframe>