Android 11 三方应用监听关机广播ACTION_SHUTDOWN

前言

最近有项目过程中,有做app的同事反馈,三方应用无法监听关机广播。特地研究了下关机广播为啥监听不到。

1.原因:发送关机广播的类是ShutdownThread.java,添加了flag:Intent.FLAG_RECEIVER_FOREGROUND | Intent.FLAG_RECEIVER_REGISTERED_ONLY,表示只有在代码中动态注册,并且是前台服务和应用才能收到,所以在AndroidManifest.xml注册无法收到关机广播,后台服务中动态注册也无法收到。

2.前台服务注册关机广播。

(1).启动前台服务:

复制代码
public class BootCompleteReceiver extends BroadcastReceiver {
    private static final String TAG = "BootCompleteReceiver";
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent != null) {
            if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
//                ComponentName powerService = new ComponentName("com.android.mytest", "com.android.mytest.PowerService");
//                Intent mIntent = new Intent();
//                mIntent.setComponent(powerService);
                Intent powerServiceIntent = new Intent(context, PowerService.class);
                context.startForegroundService(powerServiceIntent);
                Log.d(TAG, "startForegroundService");
            }
        }
    }
}

(2)、添加前台服务权限,配置相关属性:

权限:

复制代码
 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

服务配置:

复制代码
 <service
            android:name=".PowerService"
            android:foregroundServiceType="mediaPlayback"
            android:enabled="true"
            android:exported="false" >
            <intent-filter>
                <action android:name="com.gwm.car.PowerService"/>
            </intent-filter>
        </service>

(3).注册关机广播:

复制代码
package com.android.mytest;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;

public class PowerService extends Service {

    private static final String TAG = "PowerService";
    public ShutdownBroadcastReceiver mShutdownBroadcastReceiver;

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate");
        mShutdownBroadcastReceiver = new ShutdownBroadcastReceiver();
    }

    private Notification getNotification() {
        NotificationChannel channel = new NotificationChannel("channel_id", "channel_name", NotificationManager.IMPORTANCE_DEFAULT);
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (manager != null) {
            manager.createNotificationChannel(channel);
        }
        return new Notification.Builder(this, "channel_id")
                .setContentTitle("shutdown")
                .setContentText("Listening for shutdown")
//                .setAutoCancel(true)
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .build();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand");
        startForeground(1, getNotification());
        registerBroadcast();
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy");
        unregisterBroadcast();
        stopForeground(true);
        stopSelf();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    public void registerBroadcast() {
        Log.d(TAG, "registerBroadcast");
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction((Intent.ACTION_SHUTDOWN));
        registerReceiver(mShutdownBroadcastReceiver,intentFilter);

    }

    public void unregisterBroadcast() {
        if (mShutdownBroadcastReceiver != null) {
            unregisterReceiver(mShutdownBroadcastReceiver);
        }
    }
}

(4).关机广播实现

复制代码
package com.android.mytest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class ShutdownBroadcastReceiver extends BroadcastReceiver {
    private static final String TAG = "ShutdownBroadcastReceiver";
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "zjy onReceive intent:"+intent);
    }
}

(5).本地验证:

相关推荐
没有了遇见4 小时前
Android 原生定位(替代高德 / 百度等三方定位)<终极版本>
android
2501_916008895 小时前
iOS 抓包工具有哪些?全面盘点主流工具与功能对比分析
android·ios·小程序·https·uni-app·iphone·webview
2501_915921435 小时前
iOS混淆工具实战 视频流媒体类 App 的版权与播放安全保护
android·ios·小程序·https·uni-app·iphone·webview
CYRUS_STUDIO6 小时前
LLVM 全面解析:NDK 为什么离不开它?如何亲手编译调试 clang
android·编译器·llvm
CYRUS_STUDIO6 小时前
静态分析神器 + 动态调试利器:IDA Pro × Frida 混合调试实战
android·逆向
g_i_a_o_giao8 小时前
Android8 binder源码学习分析笔记(一)
android·java·笔记·学习·binder·安卓源码分析
翻滚丷大头鱼8 小时前
android 四大组件—BroadcastReceiver
android
人生游戏牛马NPC1号9 小时前
学习 Android (二十) 学习 OpenCV (五)
android·opencv·学习
2501_916008899 小时前
uni-app iOS 日志与崩溃分析全流程 多工具协作的实战指南
android·ios·小程序·https·uni-app·iphone·webview
文 丰9 小时前
【AndroidStudio】官网下载免安装版,AndroidStudio压缩版的配置和使用
android