android-學習篇-BroadcastReceiver(廣播)

簡介

Android 應用與系統和其他應用之間可以相互收發廣播消息,這與發佈-訂閱設計模式相似。這些廣播會在所關注的事件發生時發送。

接收廣播

由於接收器的 onReceive(Context, Intent) 方法在主線程上運行,因此它會快速執行並返回。廣播接收操作在10秒內完成。
方式一 清單文件中聲名

    <receiver android:name=".MyBroadcastReceiver"  android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
            <action android:name="android.intent.action.INPUT_METHOD_CHANGED" />
        </intent-filter>
    </receiver>

創建 BroadcastReceiver 子類並實現 onReceive(Context, Intent)。

方式二 上下文註冊

BroadcastReceiver br = new MyBroadcastReceiver();

IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
this.registerReceiver(br, filter);// 註冊

// 停止接收廣播,請調用 unregisterReceiver(android.content.BroadcastReceiver)。
// 當您不再需要接收器或上下文不再有效時,請務必註銷接收器。

只要註冊上下文有效,上下文註冊的接收器就會接收廣播。例如,如果您在 Activity 上下文中註冊,只要 Activity 沒有被銷燬,您就會收到廣播。如果您在應用上下文中註冊,只要應用在運行,您就會收到廣播。
請注意註冊和註銷接收器的位置,比方說,如果您使用 Activity 上下文在 onCreate(Bundle) 中註冊接收器,則應在 onDestroy() 中註銷,以防接收器從 Activity 上下文中泄露出去。如果您在 onResume() 中註冊接收器,則應在 onPause() 中註銷,以防多次註冊接收器(如果您不想在暫停時接收廣播,這樣可以減少不必要的系統開銷)。請勿在 onSaveInstanceState(Bundle) 中註銷,因爲如果用戶在歷史記錄堆棧中後退,則不會調用此方法。

發送廣播

// 一次向一個接收器發送廣播
sendOrderedBroadcast(Intent, String)
// 按隨機的順序向所有接收器發送廣播
sendBroadcast(Intent) 
// 將廣播發送給與發送器位於同一應用中的接收器。如果不需要跨應用發送廣播,請使用本地廣播。這種實現方法的效率更高(無需進行進程間通信),而且您無需擔心其他應用在收發您的廣播時帶來的任何安全問題。
LocalBroadcastManager.sendBroadcast 
Intent intent = new Intent();
intent.setAction("com.example.broadcast.MY_NOTIFICATION");
intent.putExtra("data", "Notice me senpai!");
sendBroadcast(intent);

通過權限限制廣播

帶權限的發送

sendBroadcast(new Intent("com.example.NOTIFY"), Manifest.permission.SEND_SMS);

// 接收方必須申請權限
// 可以指定現有的系統權限,也可以使用 <permission> 元素定義自定義權限。
<uses-permission android:name="android.permission.SEND_SMS"/>

注意:自定義權限將在安裝應用時註冊。定義自定義權限的應用必須在使用自定義權限的應用之前安裝。

帶權限的接收

<receiver android:name=".MyBroadcastReceiver"
              android:permission="android.permission.SEND_SMS">
        <intent-filter>
            <action android:name="android.intent.action.AIRPLANE_MODE"/>
        </intent-filter>
    </receiver>
    IntentFilter filter = new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
    registerReceiver(receiver, filter, Manifest.permission.SEND_SMS, null );

發送方需要請求權限,才能向上面的接收器發送廣播

<uses-permission android:name="android.permission.SEND_SMS"/>

對進程狀態的影響

不應從廣播接收器啓動長時間運行的後臺線程。onReceive() 完成後,系統可以隨時終止進程來回收內存,在此過程中,也會終止進程中運行的派生線程。要避免這種情況,您應該調用 goAsync()(如果您希望在後臺線程中多花一點時間來處理廣播)或者使用 JobScheduler 從接收器調度 JobService,這樣系統就會知道該進程將繼續活躍地工作。

    public class MyBroadcastReceiver extends BroadcastReceiver {
        private static final String TAG = "MyBroadcastReceiver";

        @Override
        public void onReceive(Context context, Intent intent) {
            final PendingResult pendingResult = goAsync();
            Task asyncTask = new Task(pendingResult, intent);
            asyncTask.execute();
        }

        private static class Task extends AsyncTask<String, Integer, String> {

            private final PendingResult pendingResult;
            private final Intent intent;

            private Task(PendingResult pendingResult, Intent intent) {
                this.pendingResult = pendingResult;
                this.intent = intent;
            }

            @Override
            protected String doInBackground(String... strings) {
                StringBuilder sb = new StringBuilder();
                sb.append("Action: " + intent.getAction() + "\n");
                sb.append("URI: " + intent.toUri(Intent.URI_INTENT_SCHEME).toString() + "\n");
                String log = sb.toString();
                Log.d(TAG, log);
                return log;
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                // Must call finish() so the BroadcastReceiver can be recycled.
                pendingResult.finish();
            }
        }
    }

參考

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章