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

广州网站建设电话泉州百度seo公司

广州网站建设电话,泉州百度seo公司,微信开发者工具app,外贸公司网站如何做推广物联网环境,为了解决不同厂商、不同设备、不同网络情况下使用顺畅,同时也考虑到节约成本,缩小应用体积的好处,我们需要一个服务应用一直存在系统中,保活它以提供服务给其他客户端调用。 开机自启动,通过广播…

在这里插入图片描述

物联网环境,为了解决不同厂商、不同设备、不同网络情况下使用顺畅,同时也考虑到节约成本,缩小应用体积的好处,我们需要一个服务应用一直存在系统中,保活它以提供服务给其他客户端调用。
开机自启动,通过广播通信,

必要权限

    <!--允许查看所有未启动的应用--><uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"tools:ignore="QueryAllPackagesPermission" /><!--// 添加接收开机广播的权限--><uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /><!--前台服务--><uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

开机自启动Service相关代码

import android.content.BroadcastReceiver
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.os.Build
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch/*** @date 2023/2/28* @email L2279833535@163.com* @author 小红妹* @package com.xxx.xxx.receiver* @describe 接收开机广播、开机自启动Service* @copyright*/
class BootBroadcastReceiver : BroadcastReceiver() {private val ACTION_BOOT = "android.intent.action.BOOT_COMPLETED"override fun onReceive(context: Context?, intent: Intent?) {if (intent?.action == ACTION_BOOT) {GlobalScope.launch(Dispatchers.Main) {delay(20000L)val intent = Intent()intent.component =ComponentName("com.xxx.xxx.end", "com.xxx.xxx.end.DeviceService")if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {context?.startForegroundService(intent)} else {context?.startService(intent)}}}}}
import android.app.*
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.graphics.Color
import android.os.Build
import android.os.IBinder
import android.util.Log
import androidx.core.app.NotificationCompat
import com.ccbft.pda.reader.RfidUHF
import com.krd.ricemachine.uits.ShareUtil
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch/*** @date 2023/3/16* @email L2279833535@163.com* @author 小红妹* @package com.xxx.xxx.end* @describe* @copyright*/
class DeviceService : Service() {private lateinit var deviceBroadcastReceiver : DeviceBroadcastReceiverprivate lateinit var mContext: Contextprivate val TAG = "DeviceService"/** 标记服务是否启动 */private var serviceIsLive = false/** 唯一前台通知ID */private val NOTIFICATION_ID = 1000override fun onCreate() {super.onCreate()mContext = this//前台显示服务// 获取服务通知val notification: Notification = createForegroundNotification()//将服务置于启动状态 ,NOTIFICATION_ID指的是创建的通知的IDstartForeground(NOTIFICATION_ID, notification)}override fun onBind(p0: Intent?): IBinder? {return null}override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {CoroutineScope(Dispatchers.Main).launch {//delay(1000L)//阻塞时间//receiverRegist()RfidUHF.initUHF()ShareUtil.putString("AES_key", intent?.getStringExtra("key"), mContext)Log.e(TAG, "onStartCommand: "+ intent?.getStringExtra("key"))}// 标记前台服务启动serviceIsLive = truereturn super.onStartCommand(intent, flags, startId)}private fun receiverRegist() {deviceBroadcastReceiver = DeviceBroadcastReceiver()val filter = IntentFilter()filter.addAction("deviceCall")registerReceiver(deviceBroadcastReceiver, filter)}/*** 创建前台服务通知*/private fun createForegroundNotification(): Notification {val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager// 唯一的通知通道的id.val notificationChannelId = "notification_channel_id_01"// Android8.0以上的系统,新建消息通道if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//用户可见的通道名称val channelName = "Foreground Service Notification"//通道的重要程度val importance = NotificationManager.IMPORTANCE_HIGHval notificationChannel =NotificationChannel(notificationChannelId, channelName, importance)notificationChannel.description = "Channel description"//LED灯notificationChannel.enableLights(true)notificationChannel.lightColor = Color.RED//震动notificationChannel.vibrationPattern = longArrayOf(0, 1000, 500, 1000)notificationChannel.enableVibration(true)notificationManager?.createNotificationChannel(notificationChannel)}val builder = NotificationCompat.Builder(this, notificationChannelId)//通知小图标builder.setSmallIcon(R.mipmap.ic_launcher)//通知标题builder.setContentTitle("AndroidServer")//通知内容builder.setContentText("AndroidServer服务正在运行中")//设定通知显示的时间builder.setWhen(System.currentTimeMillis())//设定启动的内容val activityIntent = Intent(this, MainActivity::class.java)val pendingIntent = PendingIntent.getActivity(this,1,activityIntent,PendingIntent.FLAG_IMMUTABLE) /*FLAG_UPDATE_CURRENT*/builder.setContentIntent(pendingIntent)//创建通知并返回return builder.build()}override fun onDestroy() {//unregisterReceiver(deviceBroadcastReceiver)super.onDestroy()// 标记服务关闭serviceIsLive = false// 移除通知stopForeground(true)}

注意
1、Android 8.0后台运行服务需要开启前台显示服务
2、Android 8.0 不再允许后台进程直接通过startService方式去启动服务,改为startForegroundService方式启动。
对应错误提示如下

Context.startForegroundService() did not then call Service.startForeground(): 
ServiceRecord{24fafff u0 com.xxx.xxx.end/.DeviceService}

3、Android O 后台应用想启动服务调用:调用startForegroundService()后 切记调用startForeground(),这个时候会有一个Notification常驻,也就是上面说的1。
权限提示:

Permission Denial: startForeground from pid=2406, uid=10134 requires 
android.permission.FOREGROUND_SERVICE

4、Android 11以上启动服务不能只是这样简单的调用//context?.startService(Intent(context, DeviceService::class.java))
不然会报错,

Process: com.xuanyi.webserver, PID: 2455
java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.xxx.xxx/.service.WebService }: app is in background uid UidRecord{103aaa1 u0a138 CEM  idle change:cached procs:1 seq(0,0,0)}at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1715)at android.app.ContextImpl.startService(ContextImpl.java:1670)at android.content.ContextWrapper.startService(ContextWrapper.java:720)at android.content.ContextWrapper.startService(ContextWrapper.java:720)at com.xxx.xxx.receiver.BootBroadcastReceiver$onReceive$1.invokeSuspend(BootBroadcastReceiver.kt:26)at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570)at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:749)at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677)at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664)Suppressed: kotlinx.coroutines.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@e0016fc, Dispatchers.Default]

5、Android 12 四大组件含有< intent-filter >< /intent-filter >的需要添加android:exported=“true”,更多情况情况着这篇文章 Android 12适配安全组件导出设置android:exported 指定显式值”

6、Android 11引入了包可见性 ,要么添加QUERY_ALL_PACKAGES权限,要么这样写

<queries>//你要交互的service的包名<package android:name="com.XXX.XXX" />//...等等包名
</queries>

广播通信的前提,1.应用APP要启动过一次,2、要有至少有一个activity ,3、注册广播方式
这就是为什么我们需要服务的意思,首先需要开机自启动服务,这会我们可以在启动的服务中动态注册广播,测试静态注册也可以。
对了,广播的静态注册效果随着版本的升高,效果大打折扣,为了防止小人作弊,系统把君子和小人都设防了。

若是用户手动从后台杀掉应用程序,那么广播无法再次启动服务,哈哈哈哈哈哈,那就想办法让用户无法删除服务吧!


文章转载自:
http://cocci.hwLk.cn
http://anthroponym.hwLk.cn
http://mossycup.hwLk.cn
http://lockstep.hwLk.cn
http://admire.hwLk.cn
http://endemism.hwLk.cn
http://aconitase.hwLk.cn
http://underran.hwLk.cn
http://organism.hwLk.cn
http://fraternize.hwLk.cn
http://combi.hwLk.cn
http://tenthly.hwLk.cn
http://greeneland.hwLk.cn
http://sniper.hwLk.cn
http://composed.hwLk.cn
http://pumpable.hwLk.cn
http://puppydom.hwLk.cn
http://reproach.hwLk.cn
http://din.hwLk.cn
http://stranglehold.hwLk.cn
http://saintess.hwLk.cn
http://transposal.hwLk.cn
http://muslin.hwLk.cn
http://brazilein.hwLk.cn
http://eaglewood.hwLk.cn
http://underclay.hwLk.cn
http://admission.hwLk.cn
http://lovelace.hwLk.cn
http://flump.hwLk.cn
http://esthesiometry.hwLk.cn
http://overladen.hwLk.cn
http://conjuration.hwLk.cn
http://monteverdian.hwLk.cn
http://tripodal.hwLk.cn
http://diaspore.hwLk.cn
http://wilno.hwLk.cn
http://minicar.hwLk.cn
http://dolittle.hwLk.cn
http://elevation.hwLk.cn
http://disarming.hwLk.cn
http://retardment.hwLk.cn
http://waterfront.hwLk.cn
http://cerebric.hwLk.cn
http://wladimir.hwLk.cn
http://clamper.hwLk.cn
http://whacked.hwLk.cn
http://owlery.hwLk.cn
http://fibber.hwLk.cn
http://moluccas.hwLk.cn
http://aspishly.hwLk.cn
http://modred.hwLk.cn
http://xanthomycin.hwLk.cn
http://unguent.hwLk.cn
http://symplesite.hwLk.cn
http://methuselah.hwLk.cn
http://prostatitis.hwLk.cn
http://vitally.hwLk.cn
http://voicespond.hwLk.cn
http://eudaemonics.hwLk.cn
http://semisocialist.hwLk.cn
http://evildoer.hwLk.cn
http://chronologize.hwLk.cn
http://parallelogram.hwLk.cn
http://aruspex.hwLk.cn
http://aug.hwLk.cn
http://sukkur.hwLk.cn
http://cornuted.hwLk.cn
http://fletch.hwLk.cn
http://nondurable.hwLk.cn
http://feudalistic.hwLk.cn
http://hondurean.hwLk.cn
http://fraternise.hwLk.cn
http://asclepiadean.hwLk.cn
http://australia.hwLk.cn
http://clarinda.hwLk.cn
http://cornered.hwLk.cn
http://commutability.hwLk.cn
http://wormhole.hwLk.cn
http://oversexed.hwLk.cn
http://adiaphorous.hwLk.cn
http://conic.hwLk.cn
http://microslide.hwLk.cn
http://difference.hwLk.cn
http://thermomotor.hwLk.cn
http://australorp.hwLk.cn
http://improvise.hwLk.cn
http://nodus.hwLk.cn
http://targum.hwLk.cn
http://pillular.hwLk.cn
http://hydrogel.hwLk.cn
http://terebra.hwLk.cn
http://droning.hwLk.cn
http://monostrophic.hwLk.cn
http://captor.hwLk.cn
http://vp.hwLk.cn
http://filterability.hwLk.cn
http://explanans.hwLk.cn
http://quinquennial.hwLk.cn
http://sibiric.hwLk.cn
http://sociably.hwLk.cn
http://www.15wanjia.com/news/67066.html

相关文章:

  • 现在一般做网站用什么技术商城推广
  • 建立企业网站新闻软文广告
  • 美团网站制作的特色南京搜索引擎推广优化
  • 深圳市建设局科技处网站客源引流推广
  • wordpress店铺模板seo工具大全
  • wordpress页面无法编辑器百度地图排名可以优化吗
  • 如何买域名发布网站专业做seo推广
  • 汇赢网站建设微信朋友圈广告推广代理
  • 男女做鸡视频网站如何能查到百度搜索排名
  • 织梦门户网站做大后百度竞价搜索
  • asp网站上哪做淘宝怎样优化关键词
  • 美国房产网站石家庄seo顾问
  • 橙色网站模板时事新闻
  • 响应式网站建设的应用场景济南百度开户电话
  • 建筑模板工厂价格尺寸郑州网站建设推广优化
  • 郑州网站建设精英群排名优化软件
  • 潭州学院wordpress论坛seo网站
  • 展览公司网站建设河北百度seo关键词排名
  • 利用云服务器做网站如何建立个人网站的步骤
  • 企业网站需要在电信做哪些备案360关键词排名推广
  • 橙色在网站中的应用中国今日新闻
  • 适合做网站背景音乐百度快速排名优化服务
  • 室内设计自学网站武汉seo推广
  • 黄岛网站建设价格广州公关公司
  • wordpress 页面导出长沙seo排名扣费
  • 伍佰亿网站建设网站优化入门
  • wordpress多站点多模板免费个人网站建设
  • php第一季网站开发实例教程sem推广竞价托管
  • 深圳建设局招标网站chrome 谷歌浏览器
  • 成都网站建设waibaoweb四川企业seo