廣播(Broadcast)---《第一行代碼Android》筆記

1.簡介

Android程序可以對自己感興趣的廣播註冊,程序只接受註冊的廣播,而且可以接收來自系統和其他程序的廣播。
不要在廣播中添加過多邏輯或進行任何的耗時操作,因爲在廣播接收器中不允許開啓線程。當onReceive()方法運行了較長時間而沒有結束,程序就會報錯。廣播接收器一般扮演打開程序其他組件的角色,比如創建一條通知、啓動一個服務等。
發送廣播使用Intent,接收廣播使用廣播接收器(Broadcast Receiver)。
- 標準廣播:異步執行的廣播,所有廣播接收器同時接收廣播,無先後順序之分。
- 有序廣播:同步執行的廣播,按先後順序一個一個接收廣播,一個廣播接收執行完後再傳給後一個廣播接收器,廣播接收器可以截斷廣播。

2. 廣播註冊方式

2.1 靜態註冊 在AndroidManifest.xml中註冊

可以接收開機啓動廣播:
BootCompleteReceiver廣播接收器:

package com.example.broadcasttest;

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

public class BootCompleteReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Boot Complete", Toast.LENGTH_LONG).show();
    }
}

註冊廣播:

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

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <!--開機廣播權限-->
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

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

        <!--註冊的廣播-->
        <receiver
            android:name=".BootCompleteReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <!--接收的廣播類型-->
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
    </application>
</manifest>

2.2. 動態註冊 在代碼中註冊

動態註冊雖然靈活,但是必須啓動程序才能接收廣播。
創建一個廣播接收器,只需新建一個類,繼承BroadcastReceiver類,並重寫onReceive()方法就行了。有廣播來時,就會執行onReceive()中的方法。

package com.example.broadcasttest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private IntentFilter intentFilter;
    private LocalReceiver localReceiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        intentFilter = new IntentFilter();//創建IntentFilter實例
        intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");//確定接收的廣播種類,這裏是網絡發生變化的廣播
        localReceiver = new LocalReceiver();//創建廣播接收器實例
        registerReceiver(localReceiver, intentFilter); // 註冊本地廣播監聽器,這樣LocalReceiver就會收到所有值爲android.net.conn.CONNECTIVITY_CHANGE的廣播,也就實現了監聽網絡變化的功能
    }
    /**
    *動態註冊的廣播必須取消註冊
    */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(localReceiver);//取消註冊
    }

    /**
     * 這個是廣播類繼承了BroadcastReceiver類
     */
    class LocalReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            //getSystemService()方法得到ConnectivityManager的實例,這是一個系統服務,專門用於管理網絡連接的。
            ConnectivityManager connectionManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
            //getActiveNetworkInfo()方法得到NetworkInfo實例
            NetworkInfo networkInfo = connectionManager.getActiveNetworkInfo();
            //NetworkInfo的isAvailable()方法用以判斷當前是否網絡
            if (networkInfo != null && networkInfo.isAvailable()) {
                Toast.makeText(context, "network is available",
                        Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(context, "network is unavailable",
                        Toast.LENGTH_SHORT).show();
            }
        }
    }
}

在AndroidManifest.xml中打開權限:

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

3. 自定義廣播

3.1 發送標準廣播

//定義發送的廣播action,這裏的com.example.broadcasttest.LOCAL_BROADCAST是自定義的值,只需新建一個廣播接收器接收這個值得廣播,就能接收到這裏發送的廣播
Intent intent = new Intent("com.example.broadcasttest.LOCAL_BROADCAST");
sendBroadcast(intent); // 發送本地廣播

3.2 發送有序廣播

//定義發送的廣播action
Intent intent = new Intent("com.example.broadcasttest.LOCAL_BROADCAST");
sendOrderedBroadcast(intent,null); // 發送有序廣播廣播.第一個參數是intent,第二個參數是與權限有關的字符串,這裏傳入null

註冊廣播時需要定義順序值android:priority:

<receiver
    android:name=".MyBroadcastReceiver"
    android:enabled="true"
    android:exported="true">
    <intent-filter android:priority="100"><!--這裏確定該廣播接收器接收廣播的優先級,值越大越先接收-->
        <action android:name="com.example.broadcasttest.MY_BROADCAST"/>
    </intent-filter>
</receiver>

在廣播接收器的onReceive()中調用abortBroadcast()發放截斷有序廣播。:

package com.example.broadcasttest;

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

public class MyBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "received in MyBroadcastReceiver", Toast.LENGTH_SHORT).show();
        abortBroadcast();//截斷有序廣播,這之後的廣播接收器就收不到廣播
    }
}

4 本地廣播

只在程序內部傳播的廣播,避免安全性問題,而且本地廣播無法通過靜態註冊的方式接收。本地廣播使用LocalBroadcastManager類來對廣播進行管理,並提供了光送廣播和註冊廣播接收器的方法:

package com.example.broadcasttest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private IntentFilter intentFilter;
    private LocalReceiver localReceiver;
    private LocalBroadcastManager localBroadcastManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        localBroadcastManager = LocalBroadcastManager.getInstance(this); // 獲取實例
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("com.example.broadcasttest.LOCAL_BROADCAST");
                localBroadcastManager.sendBroadcast(intent); // 發送本地廣播
            }
        });
        intentFilter = new IntentFilter();
        intentFilter.addAction("com.example.broadcasttest.LOCAL_BROADCAST");
        localReceiver = new LocalReceiver();
        localBroadcastManager.registerReceiver(localReceiver, intentFilter); // 註冊本地廣播監聽器
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        localBroadcastManager.unregisterReceiver(localReceiver);//註銷本地廣播
    }

    /**
     * 這個是廣播類繼承了BroadcastReceiver類
     */
    class LocalReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context, "received local broadcast", Toast.LENGTH_SHORT).show();
        }

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