android-BroadcastReceiver 通过终止广播 阻止用户收到短信

1,系统收到短信,发出的广播属于有序广播。

如果想阻止用户收到短信,可自定义Receiver,设置高优先级,率先获得接收短信的广播,并终止广播。

2,接收短信的广播名android.provider.Telephony.SMS_RECEIVED

3,注意:程序一旦在某个模拟器运行,将一直阻止短信,只有注释掉abortBroadcast(),重新运行,方可正常。


manifest

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

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".C49_BroadcastSmsActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

主activity

package com.song;

import android.app.Activity;
import android.os.Bundle;

public class C49_BroadcastSmsActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

receiver

package com.song;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MySmsResevicer extends BroadcastReceiver{

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		System.out.println("on receive");
		abortBroadcast();
	}
	
}



    原文作者:superjunjin
    原文地址: https://blog.csdn.net/superjunjin/article/details/7487669
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞