【Android 系統開發】_「四大組件」篇 -- 「動態廣播」 和 「靜態廣播」

動態註冊

Code

新建一個 BroadcastTest 項目,修改 MainActivity :java

package com.example.marco.broadcasttest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private IntentFilter intentFilter;

    private NetworkChangeReceiver networkChangeReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        intentFilter = new IntentFilter();
        intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        networkChangeReceiver = new NetworkChangeReceiver();
        registerReceiver(networkChangeReceiver, intentFilter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(networkChangeReceiver);
    }


    private class NetworkChangeReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context, "network changes", Toast.LENGTH_LONG).show();
        }
    }
}

靜態註冊

「動態註冊」的廣播接收器能夠自由地控制註冊與註銷,在靈活性方面有很大的優點,可是它也存在一個缺點:必需要在程序啓動以後才能接收到廣播,由於註冊的邏輯是寫在 onCreate() 方法中的。android

那麼有沒有什麼方法能夠讓程序在未啓動的狀況下就能接收到廣播呢?這就是咱們即將看到的「靜態註冊」方法了!app

Code

(1)新建一個 BroadcastReceiver:ide

package com.example.marco.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();
    }
}

(2)靜態廣播接收器必定要在 AndroidManifest.xml 文件中註冊才能夠使用,若是你使用 Android Studio 快捷方式建立了這個廣播接收器,那麼系統會自動在 AndroidManifest.xml 中註冊好這個廣播接收器:.net

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

    <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>

(3)此時廣播接收器還未能接收到開機廣播,須要添加 Action 和 權限:code

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

    // 添加權限
    <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">
            // 添加相應的 Action
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
    </application>

</manifest>
相關文章
相關標籤/搜索