Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏

126.2. 静态注册

Android Studio 选择 File - New - Other - Broadcast Receiver

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

    <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=".MyReceiver"
            android:enabled="true"
            android:exported="true">

            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>

        </receiver>
    </application>

</manifest>		
		
		

MyReceiver 集成 BroadcastReceiver 在 onReceive 中写入你的业务逻辑。

		
package cn.netkiller.broadcast;

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

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "程序已启动,接收到系统启动广播", Toast.LENGTH_SHORT).show();
    }
}
		
		
		

现在重启 Android 模拟器,启动后虽然 App 并没有进入,但是屏幕底部会看到 "程序已启动,接收到系统启动广播"

126.2.1. 电源管理

静态注册

		
        <receiver
            android:name=".receiver.StaticBroadcastReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter android:priority="1000">
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.ACTION_BATTERY_CHANGED" />
                <action android:name="android.intent.action.ACTION_BATTERY_LOW" />
                <action android:name="android.intent.action.ACTION_BATTERY_OKAY" />
                <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
                <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>		
		
			

动态注册

		
            IntentFilter filter = new IntentFilter();
            filter.addAction(Intent.ACTION_BATTERY_CHANGED);
            filter.addAction(Intent.ACTION_BATTERY_LOW);
            filter.addAction(Intent.ACTION_BATTERY_OKAY);
            filter.addAction(Intent.ACTION_POWER_CONNECTED);
            filter.addAction(Intent.ACTION_POWER_DISCONNECTED);		
		
			

126.2.2. 接收不到消息

Android 8 以上,静态广播必须指定包名 intent.setPackage(context.getPackageName());

			
    public static void broadcastTest(String message) {
        Log.d(TAG, "发送广播: " + message);
        Context context = ContextHolder.getContext();
        Intent intent = new Intent();
        intent.setAction("test.broadcast");
        intent.setPackage(context.getPackageName());
        intent.putExtra("message", message);
        context.sendBroadcast(intent);
    }