技術學習記錄

[Android]四大元件之Broadcast Receiver

關於Broadcast Receiver

所謂的Broadcast Receiver,即是負責收發廣播的元件

當系統發生一些事件,比如說開機完成、連接網路、充電時,Broadcast Receiver會接收到這些事情並讓我們做相應的處理。

Broadcast Receiver可以在Activity內運作,也可以獨立運作。

例如當我想要偵測手機是否在充電狀態,並在Activity中顯示,那麼我可以在Activity註冊一個Broadcast Receiver元件,並在onReceive的callback中處理即可。

關於Intent Filter

在Android系統中,許多系統事件都會透過廣播的形式發送出來。

以前面提到的例子說明:我只需要知道電池狀態的事件就好,但Android提供的事件太多,我總不可能全部接收下來做處理吧?

因此我們可以透過Intent Filter來過濾掉我只想要的系統事件,例如電池狀態,我們可以在Intent Filter中加入Intent.ACTION_BATTERY_CHANGED這個intent action,來過濾這個事件。

使用範例

1.動態註冊方式

這裡用偵測充電器插入或拔除狀態作為示範,程式碼如下:

public class MainActivity extends AppCompatActivity {

    private BroadcastReceiver mReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mReceiver = getBroadcastReceiver();
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 當活動恢復,已經可以和使用者互動時,註冊廣播接收器
        registerReceiver(mReceiver, getIntentFilter());
    }

    @Override
    protected void onPause() {
        super.onPause();
        // 當活動暫停,且使用者無法與之互動時,解除廣播接收器
        unregisterReceiver(mReceiver);
    }

    // 建立廣播接收器,並處理事件
    private BroadcastReceiver getBroadcastReceiver() {
        return new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if (action.equals(Intent.ACTION_POWER_CONNECTED)) {
                    Toast.makeText(context, "充電器已插上", Toast.LENGTH_SHORT).show();
                } else if (action.equals(Intent.ACTION_POWER_DISCONNECTED)) {
                    Toast.makeText(context, "充電器已拔除", Toast.LENGTH_SHORT).show();
                }
            }
        };
    }

    // 建立廣播事件過濾器
    private IntentFilter getIntentFilter() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_POWER_CONNECTED);
        filter.addAction(Intent.ACTION_POWER_DISCONNECTED);
        return filter;
    }
}

28~40行:建立BroadcastReceiver物件,覆寫onReceiver方法中的事件處理

這裡會去解析intent中的action成員,如果是ACTION_POWER_CONNECT、ACTION_POWER_DISCONNECT則透過Toast顯示充電器已插上/充電器已拔除。

43~49行:建立IntentFilter物件,過濾掉其他不要的廣播,只接收ACTION_POWER_CONNECT、ACTION_POWER_DISCONNECT這兩個action。

17行:註冊廣播接收器,以及想接收的廣播事件

24行:將廣播接收器解除註冊

2.靜態註冊方式

這裡使用客製化廣播事件做為示範,程式碼及步驟如下:

首先是客製化BroadcastReceiver類別,這裡以MyBroadcastReceiver表示:

public class MyBroadcastReceiver extends BroadcastReceiver {

    // 客製化事件
    public static final String ACTION_BUTTON_PRESSED = "com.ray650128.broadcastreceiverdemo.ACTION_BUTTON_PRESSED";

    private static final String TAG = "MyBroadcastReceiver";

    public MyBroadcastReceiver() {
        Log.i(TAG, "MyBroadcastReceiver 已啟動");
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // 接收到廣播事件,進行解析
        String action = intent.getAction();
        if (action.equals(ACTION_BUTTON_PRESSED)) {
            Toast.makeText(context, "有人偷按按鈕!", Toast.LENGTH_SHORT).show();
        }
        Log.i(TAG, "接收到廣播" + action);
    }
}

在AndroidManifest.xml文件中加入MyBroadcastReceiver,並加上IntentFilter:

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.BroadcastReceiverDemo">
        <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=".MyBroadcastReceiver">
            <intent-filter>
                <action android:name="com.ray650128.broadcastreceiverdemo.ACTION_BUTTON_PRESSED" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

最後在MainActivity.java中加入發送廣播的程式碼,如下:

public class MainActivity extends AppCompatActivity {

    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button) findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 發送廣播
                Intent intent = new Intent();
                intent.setAction(MyBroadcastReceiver.ACTION_BUTTON_PRESSED);
                sendBroadcast(intent);
            }
        });
    }
}

以上是BroadcastReceiver的兩種方式僅供參考。

關於Broadcast Receiver需要注意的地方

1.不要在Broadcast Receiver中執行耗時的操作

由於Broadcast Receiver的生命週期很短,從onReceive()方法開始執行到結束的時間大約10秒,之後就會被系統銷毀,所以在onReceive()方法盡量不要做耗時的操作,不然可能會ANR,也就是系統會提示你「應用程式沒有回應」。

其餘項目之後再補充

以上則是繼Android四大物件簡介之後,對Broadcast Receiver更詳細一點的介紹,如果有興趣更深入探討,可以參考Android Developers網站

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *