靜態註冊實現開機啓動

本文來自郭霖大神的《第一行代碼》。
鄙人不才,抄作筆記,以供回顧,刪改之處,還望見諒。

動態註冊的廣播接收器可以自由地控制註冊和註銷,靈活性好,但有一個缺點,即必須要在程序啓動後才能接收到廣播,因爲註冊的邏輯是寫在onCreate()方法中的.
這裏我們採用靜態註冊的方式讓程序接收到一條開機廣播,收到這條廣播的時候就可以在onReceive()方法裏執行相應的邏輯,從而實現開機啓動的工功能.可以使用Android Studio 提供的快捷方式創建一個廣播接收器.
,右擊com.example.broadcasttest包 ->News ->Other ->Broadcast Receiver
這裏寫圖片描述

這裏寫圖片描述

這裏我們將廣播接收器命名爲BootCompleteReceiver , Exported屬性表示是否允許這個廣播接收器接收本程序以外的廣播,Enabled屬性表示是否啓用這個廣播接收器,都勾選,Finish.
然後修改BootCompleteReceiver的代碼

package com.example.luokexi.broadcasttestdemo2;

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

public class BootCompleteReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        //throw new UnsupportedOperationException("Not yet implemented");
        Toast.makeText(context,"Boot Complete",Toast.LENGTH_SHORT).show();
    }
}

這裏這是在onReceive()方法中使用Toast彈出一段提示信息.
另外,靜態的廣播接收器一定要在AndroidManifest.xml文件中註冊才能使用,不過我們使用的是Android Studio 的快捷方式創建的廣播接收器,因此註冊這一步已經自動完成.打開AndroidManifest.xml查看下.

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

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <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/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">
        </receiver>
    </application>

</manifest>

可以看到 application標籤內出現了一個新的標籤 receiver ,所有的靜態的廣播器都是在這裏進行註冊的. 用法和 activity 標籤非常類似. 也是通過android:name來指定具體註冊哪一個廣播接收器,而enabled和exported屬性則是根據我們剛纔勾選的狀態自動生成的.

不過目前的 BootCompleteReceiver還是不能接受到開機廣播的,我們還需要對AndroidManifest.xml文件進行修改:

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

    <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:roundIcon="@mipmap/ic_launcher_round"
        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>

由於Android系統啓動完成後會發出一條值爲android.intent.action.BOOT_COMPLETED的廣播,因此我們在 intent-filter 標籤裏添加了響應的 action .另外,監聽系統開機廣播也是需要聲明權限的,可以看到我們使用 uses - permission 標籤 又加入了一條 android.permission.RECEIVE_BOOT_COMPLETED權限.
現在我們試試看. 運行程序後,將模擬器關閉,重新啓動,在啓動完成後就會收到開機廣播.
這裏寫圖片描述

我們在廣播接收器的onReceive()方法中都只是簡單地使用Toast提示了一段文本信息,當你真正在項目中使用它的時候,就可以在裏面編寫自己的邏輯.需要注意的是不要在onReceive()方法中添加過多的邏輯或者進行任何耗時的操作,因爲在廣播接收器中是不允許開啓線程的,當onReceive()方法運行較長時間而沒有結束時,程序就會報錯.因此廣播接收器更多的是扮演一種打開程序其他組件的角色,比如創建一條狀態欄通知,或者啓動一個服務等.

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