Android短信监听(一)——利用BroadcastReceiver实现短信监听

简介: MainActivity如下: package cc.testsmsbroadcastreceiver;import cc.testsmsbroadcastreceiver.

MainActivity如下:

package cc.testsmsbroadcastreceiver;
import cc.testsmsbroadcastreceiver.SMSBroadcastReceiver.MessageListener;
import android.os.Bundle;
import android.widget.TextView;
import android.app.Activity;
/**
 * Demo描述:
 * 利用BroadcastReceiver实现监听短信
 * 
 * 注意权限:
 * <uses-permission android:name="android.permission.RECEIVE_SMS"/>
 * 
 * 详细资料:
 * http://blog.csdn.net/lfdfhl/article/details/8195400
 * 
 */
public class MainActivity extends Activity{
    private TextView mTextView;
    private SMSBroadcastReceiver mSMSBroadcastReceiver;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		init();
	}
    private void init(){
    	mTextView=(TextView) findViewById(R.id.textView);
    	mSMSBroadcastReceiver=new SMSBroadcastReceiver();
    	mSMSBroadcastReceiver.setOnReceivedMessageListener(new MessageListener() {
			@Override
			public void OnReceived(String message) {
				mTextView.setText(message);
			}
		});
    }

}

SMSBroadcastReceiver如下:

package cc.testsmsbroadcastreceiver;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;
/**
 * 配置广播接收者:
 *  <receiver android:name=".SMSBroadcastReceiver">
 *     <intent-filter android:priority="1000">
 *         <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
 *     </intent-filter>
 *  </receiver>
 *  
 *  注意:
 *  <intent-filter android:priority="1000">表示:
 *  设置此广播接收者的级别为最高
 */

public class SMSBroadcastReceiver extends BroadcastReceiver {
    private static MessageListener mMessageListener;
	public SMSBroadcastReceiver() {
		super();
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		     Object [] pdus= (Object[]) intent.getExtras().get("pdus");
		     for(Object pdu:pdus){
		    	SmsMessage smsMessage=SmsMessage.createFromPdu((byte [])pdu);
		    	String sender=smsMessage.getDisplayOriginatingAddress();
		    	String content=smsMessage.getMessageBody();
		    	long date=smsMessage.getTimestampMillis();
		    	Date timeDate=new Date(date);
		    	SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		    	String time=simpleDateFormat.format(timeDate);
		    	
		    	System.out.println("短信来自:"+sender);
		    	System.out.println("短信内容:"+content);
		    	System.out.println("短信时间:"+time);
		    	
		    	mMessageListener.OnReceived(content);
		    	
		    	//如果短信来自5556,不再往下传递
		    	if("5556".equals(sender)){
		    		System.out.println(" abort ");
		    		abortBroadcast();
		    	}
		    	
		     }
	}
	
	// 回调接口
		public interface MessageListener {
			public void OnReceived(String message);
		}

		public void setOnReceivedMessageListener(MessageListener messageListener) {
			this.mMessageListener=messageListener;
		}
}

main.xml如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     >

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        android:layout_centerInParent="true" />

</RelativeLayout>

AndroidManifest.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cc.testsmsbroadcastreceiver"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="8" />
    
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="cc.testsmsbroadcastreceiver.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <receiver android:name=".SMSBroadcastReceiver">
			<intent-filter android:priority="1000">
				<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
			</intent-filter>
		</receiver>
    </application>

</manifest>


相关文章
|
2月前
|
设计模式 Android开发
[Android 四大组件] --- BroadcastReceiver
[Android 四大组件] --- BroadcastReceiver
43 0
|
21天前
|
API Android开发
Android 监听Notification 被清除实例代码
Android 监听Notification 被清除实例代码
|
27天前
|
Android开发
技术经验分享:Android前后台切换的监听
技术经验分享:Android前后台切换的监听
19 2
|
1月前
|
设计模式 Android开发
44. 【Android教程】广播接收器:Broadcast Receiver
44. 【Android教程】广播接收器:Broadcast Receiver
19 2
|
1月前
|
XML Android开发 数据格式
37. 【Android教程】基于监听的事件处理机制
37. 【Android教程】基于监听的事件处理机制
32 2
|
2月前
|
Android开发
Android监听USB设备插拔
Android监听USB设备插拔
266 7
|
2月前
|
Android开发
Android监听蓝牙广播
Android监听蓝牙广播
22 1
|
2月前
|
Android开发
Android 对鼠标事件的监听实现
Android 对鼠标事件的监听实现
35 1
|
2月前
|
Android开发
Android获取横竖屏状态及监听
Android获取横竖屏状态及监听
27 0
|
7月前
|
小程序 JavaScript 前端开发
微信小程序(十七)小程序监听返回键跳转事件(安卓返回也适用)
onUnload:function(){ wx.redirectTo({ url: '../index/index' }) wx.navigateTo({ url: '../index/index' }) wx.switchTab({ url: '../../member/member' }) }
534 0