当前位置: 首页 > news >正文

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";@Overridepublic 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" />

服务配置:

 <serviceandroid: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;@Overridepublic 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();}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.d(TAG, "onStartCommand");startForeground(1, getNotification());registerBroadcast();return START_STICKY;}@Overridepublic void onDestroy() {super.onDestroy();Log.d(TAG, "onDestroy");unregisterBroadcast();stopForeground(true);stopSelf();}@Nullable@Overridepublic 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";@Overridepublic void onReceive(Context context, Intent intent) {Log.d(TAG, "zjy onReceive intent:"+intent);}
}

(5).本地验证:

http://www.lryc.cn/news/492294.html

相关文章:

  • OpenHarmony-3.驱动HDF
  • 《白帽子讲Web安全》13-14章
  • CSS - CSS One-Line
  • gitlab ssh-key 绑定
  • wordpress使用Markdown语法写的文章图片显示不正常,记录一次折腾之旅
  • 从零开始学GeoServer源码(二)添加支持arcgis切片功能
  • WPF异步UI交互功能的实现方法
  • 网络基础 - 地址篇
  • # [Unity] 【游戏开发】Unity开发基础2-Unity脚本编程基础详解
  • Milvus实操
  • 35 基于单片机的精确电压表DA-AD转换
  • JDBC 设置 PostgreSQL 查询中 any(?) 的参数
  • 【11-20期】Java面试进阶:深入解析核心问题与实战案例
  • C++——内存池_2
  • 如何使用PHP爬虫获取店铺详情:一篇详尽指南
  • HTML5和CSS3新增特性
  • linux运行vue编译后的项目
  • 论文阅读:A Software Platform for Manipulating theCamera Imaging Pipeline
  • 【MySQL篇】持久化和非持久化统计信息的深度剖析(第一篇,总共六篇)
  • Ubuntu下安装Qt
  • 丹摩征文活动 | AI创新之路,DAMODEL助你一臂之力GPU
  • 数据库(总结自小林coding)|索引失效的场景、慢查询、原因及如何优化?undo log、redo log、binlog 作用、MySQL和Redis的区别
  • Docker容器运行CentOS镜像,执行yum命令提示“Failed to set locale, defaulting to C.UTF-8”
  • OpenCV基本图像处理操作(六)——直方图与模版匹配
  • 【LLM学习笔记】第四篇:模型压缩方法——量化、剪枝、蒸馏、分解
  • python3 自动更新的缓存类
  • 英语知识网站开发:Spring Boot框架应用
  • 文件上传upload-labs-docker通关
  • git(Linux)
  • Doris实战—构建日志存储与分析平台