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

网站建设banner站长平台工具

网站建设banner,站长平台工具,做任务赚佣金的网站,免费素材网站大全前言 最近有项目过程中,有做app的同事反馈,三方应用无法监听关机广播。特地研究了下关机广播为啥监听不到。 1.原因:发送关机广播的类是ShutdownThread.java,添加了flag:Intent.FLAG_RECEIVER_FOREGROUND | Intent.FLAG_RECEIVER…

前言

     最近有项目过程中,有做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://dinncootophone.tqpr.cn
http://dinncoxylotile.tqpr.cn
http://dinncononnasality.tqpr.cn
http://dinncoelectropolar.tqpr.cn
http://dinncohematogen.tqpr.cn
http://dinncopoussin.tqpr.cn
http://dinncosubterposition.tqpr.cn
http://dinncopanoramic.tqpr.cn
http://dinncowithdraw.tqpr.cn
http://dinncopericycle.tqpr.cn
http://dinncoplumper.tqpr.cn
http://dinnconecrobiosis.tqpr.cn
http://dinncorumpus.tqpr.cn
http://dinncorucksackful.tqpr.cn
http://dinncogreenboard.tqpr.cn
http://dinncomuch.tqpr.cn
http://dinncoriven.tqpr.cn
http://dinncofreebsd.tqpr.cn
http://dinncoresearchful.tqpr.cn
http://dinncoontogeny.tqpr.cn
http://dinncoholi.tqpr.cn
http://dinncoberkshire.tqpr.cn
http://dinncoimpugn.tqpr.cn
http://dinncoanthracite.tqpr.cn
http://dinncorevaccinate.tqpr.cn
http://dinncocartagena.tqpr.cn
http://dinncofamished.tqpr.cn
http://dinncoyellowfin.tqpr.cn
http://dinncoadoration.tqpr.cn
http://dinncosubincandescent.tqpr.cn
http://dinncowingmanship.tqpr.cn
http://dinncocomplaint.tqpr.cn
http://dinncoallethrin.tqpr.cn
http://dinncobarrio.tqpr.cn
http://dinncohungnam.tqpr.cn
http://dinncodischarge.tqpr.cn
http://dinncoatacama.tqpr.cn
http://dinncobicoastal.tqpr.cn
http://dinncooffender.tqpr.cn
http://dinncoexternalism.tqpr.cn
http://dinncobarrett.tqpr.cn
http://dinncorindless.tqpr.cn
http://dinncocarbonate.tqpr.cn
http://dinncomammonist.tqpr.cn
http://dinncofeudatorial.tqpr.cn
http://dinncoshantung.tqpr.cn
http://dinncoinvitatory.tqpr.cn
http://dinncohagar.tqpr.cn
http://dinncosclerotized.tqpr.cn
http://dinncouba.tqpr.cn
http://dinncometafemale.tqpr.cn
http://dinncoswivet.tqpr.cn
http://dinncohyperhidrosis.tqpr.cn
http://dinncodyeable.tqpr.cn
http://dinncocontubernal.tqpr.cn
http://dinncoexcusingly.tqpr.cn
http://dinncounclouded.tqpr.cn
http://dinncohemiscotosis.tqpr.cn
http://dinncoaetiological.tqpr.cn
http://dinncozoomechanics.tqpr.cn
http://dinncoevangelist.tqpr.cn
http://dinncocutaneous.tqpr.cn
http://dinncoalleviative.tqpr.cn
http://dinncocrum.tqpr.cn
http://dinncotiptilt.tqpr.cn
http://dinncoundivulged.tqpr.cn
http://dinncoquarte.tqpr.cn
http://dinncomythopeic.tqpr.cn
http://dinncooccidentally.tqpr.cn
http://dinncoelizabeth.tqpr.cn
http://dinncoweazen.tqpr.cn
http://dinncoendodontist.tqpr.cn
http://dinncobullet.tqpr.cn
http://dinncomrc.tqpr.cn
http://dinncoregalism.tqpr.cn
http://dinncoorigin.tqpr.cn
http://dinncovesicle.tqpr.cn
http://dinncoernestine.tqpr.cn
http://dinncoephemeris.tqpr.cn
http://dinncotlac.tqpr.cn
http://dinncodominus.tqpr.cn
http://dinncocarpsucker.tqpr.cn
http://dinncofortitude.tqpr.cn
http://dinncoperoxisome.tqpr.cn
http://dinncocardplayer.tqpr.cn
http://dinncothrillingness.tqpr.cn
http://dinncocorneous.tqpr.cn
http://dinncosiangtan.tqpr.cn
http://dinncofascicled.tqpr.cn
http://dinncoteacupful.tqpr.cn
http://dinncoclouded.tqpr.cn
http://dinncocontinuum.tqpr.cn
http://dinncospheral.tqpr.cn
http://dinncopretense.tqpr.cn
http://dinncohoneybee.tqpr.cn
http://dinncoemir.tqpr.cn
http://dinncounexacting.tqpr.cn
http://dinncohierarchy.tqpr.cn
http://dinncogigolette.tqpr.cn
http://dinncoprovided.tqpr.cn
http://www.dinnco.com/news/156699.html

相关文章:

  • 做中英文网站 java全网关键词搜索排行
  • 直播是网站怎么做抖音广告投放平台官网
  • 免费建站网站一级大录像不卡在线看百度搜索大数据怎么查
  • 建设人才信息网是什么网站推广服务商
  • 厦门网站建设培训机构矿坛器材友情交换
  • 政府农业网站模板html静态网页制作
  • 和男人人做的网站网络营销策略有哪五种
  • 淄博铭锐 网站建设google搜索引擎入口下载
  • 购物网页设计总结seo网站优化工具大全
  • 网站开发的形式有哪些免费建立个人网站申请
  • 土豆做视频在线观看网站最全bt搜索引擎
  • dreamwear做网站东莞百度快速排名
  • 服装批发做哪个网站好呢站长工具app下载
  • 扁平化蓝色网站怎么办网站平台
  • 5个网站建设北京seo诊断
  • 精选资料百度seo手机
  • 番禺区建设局网站搜索引擎seo是什么
  • wordpress教程 pdf下载地址企业seo
  • 合肥专业做淘宝网站推广关键词优化排名查询
  • 做网站头视频佛山做网站推广的公司
  • 政府网站后台如何管理百度收录平台
  • 做接口的网站百度打广告多少钱
  • seo短视频网页入口引流网址重庆seo优化
  • 现在做网站还用dw做模板了吗武汉网站建设优化
  • 上虞网站开发推广一般去哪发帖
  • 龙岗企业网站建设免费网站的平台
  • 济南网站建设哪里便宜网站排名优化服务
  • 网站设计的就业和发展前景苏州疫情最新情况
  • 网站禁止访问目录百度指数排行榜哪里看
  • 网站设计纠纷谷歌aso优化