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

网站建设参数最牛餐饮营销手段

网站建设参数,最牛餐饮营销手段,赣州网站建设公司,手工做火枪的网站前言 最近有项目过程中,有做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://www.dinnco.com/news/16913.html

相关文章:

  • 电器类网站设计seo关键词排名怎么优化
  • 做石材网站步骤武汉seo搜索引擎
  • 佛山专业的网站制作搜索引擎优化排名优化培训
  • 自己怎么设置网站职业培训机构资质
  • 外贸平台哪个网站最好批发南宁seo排名优化
  • python安装wordpressseo怎么优化软件
  • 项目定制开发网站世界互联网峰会
  • 淄博铭锐 网站建设天津做网站的公司
  • 扬中网站建设出售网站平台
  • 网页设计代码开头长沙网站seo推广
  • 网站导航条怎么做效果自助建站系统哪个好
  • wix做的网站在国内访问不了在线生成个人网站app
  • 电子商务网站的功能5118
  • 百度联盟的网站怎么做正规淘宝代运营去哪里找
  • 做网站需要学会哪些seo资料网
  • 腾讯云服务器可以做传奇网站吗关键词优化排名
  • 织梦cms传播公司网站模板电商运营推广是做什么的
  • 网站ui设计怎么做网站seo谷歌
  • 2003系统网站建设网站优化排名推广
  • wordpress做网站过程如何制作微信小程序
  • 澳门服务器做网站需要备案吗网店seo排名优化
  • 销售网站内容设计百度推广培训班
  • 县区级政府网站建设现状友情链接如何交换
  • 公司网站维护要做什么苏州seo培训
  • 水利网站建设管理汇报seo公司 彼亿营销
  • 石家庄英文网站建设百度推广信息流有用吗
  • 一般做个网站需要多少钱互联网网络推广公司
  • 网站制作手机网站重庆森林电影简介
  • 给外国小孩 做 英语题用的网站国家认可的赚钱软件
  • 瑞安公司做网站seo优化专员工作内容