android-BroadcastReceiver 通過終止廣播 阻止使用者收到簡訊
阿新 • • 發佈:2019-02-03
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(); } }