国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

關(guān)于Android 11、12和13服務(wù)?;顔?wèn)題

這篇具有很好參考價(jià)值的文章主要介紹了關(guān)于Android 11、12和13服務(wù)?;顔?wèn)題。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

關(guān)于Android 11、12和13服務(wù)保活問(wèn)題

物聯(lián)網(wǎng)環(huán)境,為了解決不同廠商、不同設(shè)備、不同網(wǎng)絡(luò)情況下使用順暢,同時(shí)也考慮到節(jié)約成本,縮小應(yīng)用體積的好處,我們需要一個(gè)服務(wù)應(yīng)用一直存在系統(tǒng)中,保活它以提供服務(wù)給其他客戶端調(diào)用。
開機(jī)自啟動(dòng),通過(guò)廣播通信,

必要權(quán)限

    <!--允許查看所有未啟動(dòng)的應(yīng)用-->
    <uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"
        tools:ignore="QueryAllPackagesPermission" />
    <!--// 添加接收開機(jī)廣播的權(quán)限-->
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <!--前臺(tái)服務(wù)-->
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

開機(jī)自啟動(dòng)Service相關(guān)代碼

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 接收開機(jī)廣播、開機(jī)自啟動(dòng)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 : DeviceBroadcastReceiver
    private lateinit var mContext: Context
    private val TAG = "DeviceService"
    /** 標(biāo)記服務(wù)是否啟動(dòng) */
    private var serviceIsLive = false
    /** 唯一前臺(tái)通知ID */
    private val NOTIFICATION_ID = 1000

    override fun onCreate() {
        super.onCreate()
        mContext = this
        //前臺(tái)顯示服務(wù)
        // 獲取服務(wù)通知
        val notification: Notification = createForegroundNotification()
        //將服務(wù)置于啟動(dòng)狀態(tài) ,NOTIFICATION_ID指的是創(chuàng)建的通知的ID
        startForeground(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)//阻塞時(shí)間
            //receiverRegist()
            RfidUHF.initUHF()
            ShareUtil.putString("AES_key", intent?.getStringExtra("key"), mContext)
            Log.e(TAG, "onStartCommand: "+ intent?.getStringExtra("key"))
        }
        // 標(biāo)記前臺(tái)服務(wù)啟動(dòng)
        serviceIsLive = true
        return super.onStartCommand(intent, flags, startId)
    }

    private fun receiverRegist() {
        deviceBroadcastReceiver = DeviceBroadcastReceiver()
        val filter = IntentFilter()
        filter.addAction("deviceCall")
        registerReceiver(deviceBroadcastReceiver, filter)
    }


    /**
     * 創(chuàng)建前臺(tái)服務(wù)通知
     */
    private fun createForegroundNotification(): Notification {
        val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager

        // 唯一的通知通道的id.
        val notificationChannelId = "notification_channel_id_01"

        // Android8.0以上的系統(tǒng),新建消息通道
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //用戶可見的通道名稱
            val channelName = "Foreground Service Notification"
            //通道的重要程度
            val importance = NotificationManager.IMPORTANCE_HIGH
            val notificationChannel =
                NotificationChannel(notificationChannelId, channelName, importance)
            notificationChannel.description = "Channel description"
            //LED燈
            notificationChannel.enableLights(true)
            notificationChannel.lightColor = Color.RED
            //震動(dòng)
            notificationChannel.vibrationPattern = longArrayOf(0, 1000, 500, 1000)
            notificationChannel.enableVibration(true)
            notificationManager?.createNotificationChannel(notificationChannel)
        }
        val builder = NotificationCompat.Builder(this, notificationChannelId)
        //通知小圖標(biāo)
        builder.setSmallIcon(R.mipmap.ic_launcher)
        //通知標(biāo)題
        builder.setContentTitle("AndroidServer")
        //通知內(nèi)容
        builder.setContentText("AndroidServer服務(wù)正在運(yùn)行中")
        //設(shè)定通知顯示的時(shí)間
        builder.setWhen(System.currentTimeMillis())
        //設(shè)定啟動(dòng)的內(nèi)容
        val activityIntent = Intent(this, MainActivity::class.java)
        val pendingIntent = PendingIntent.getActivity(
            this,
            1,
            activityIntent,
            PendingIntent.FLAG_IMMUTABLE
        ) /*FLAG_UPDATE_CURRENT*/
        builder.setContentIntent(pendingIntent)

        //創(chuàng)建通知并返回
        return builder.build()
    }

    override fun onDestroy() {
        //unregisterReceiver(deviceBroadcastReceiver)
        super.onDestroy()
        // 標(biāo)記服務(wù)關(guān)閉
        serviceIsLive = false
        // 移除通知
        stopForeground(true)
    }

注意
1、Android 8.0后臺(tái)運(yùn)行服務(wù)需要開啟前臺(tái)顯示服務(wù)
2、Android 8.0 不再允許后臺(tái)進(jìn)程直接通過(guò)startService方式去啟動(dòng)服務(wù),改為startForegroundService方式啟動(dòng)。
對(duì)應(yīng)錯(cuò)誤提示如下

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

3、Android O 后臺(tái)應(yīng)用想啟動(dòng)服務(wù)調(diào)用:調(diào)用startForegroundService()后 切記調(diào)用startForeground(),這個(gè)時(shí)候會(huì)有一個(gè)Notification常駐,也就是上面說(shuō)的1。
權(quán)限提示:

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

4、Android 11以上啟動(dòng)服務(wù)不能只是這樣簡(jiǎn)單的調(diào)用//context?.startService(Intent(context, DeviceService::class.java))
不然會(huì)報(bào)錯(cuò),

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適配安全組件導(dǎo)出設(shè)置android:exported 指定顯式值”

6、Android 11引入了包可見性 ,要么添加QUERY_ALL_PACKAGES權(quán)限,要么這樣寫

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

廣播通信的前提,1.應(yīng)用APP要啟動(dòng)過(guò)一次,2、要有至少有一個(gè)activity ,3、注冊(cè)廣播方式
這就是為什么我們需要服務(wù)的意思,首先需要開機(jī)自啟動(dòng)服務(wù),這會(huì)我們可以在啟動(dòng)的服務(wù)中動(dòng)態(tài)注冊(cè)廣播,測(cè)試靜態(tài)注冊(cè)也可以。
對(duì)了,廣播的靜態(tài)注冊(cè)效果隨著版本的升高,效果大打折扣,為了防止小人作弊,系統(tǒng)把君子和小人都設(shè)防了。

若是用戶手動(dòng)從后臺(tái)殺掉應(yīng)用程序,那么廣播無(wú)法再次啟動(dòng)服務(wù),哈哈哈哈哈哈,那就想辦法讓用戶無(wú)法刪除服務(wù)吧!文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-405021.html

到了這里,關(guān)于關(guān)于Android 11、12和13服務(wù)?;顔?wèn)題的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • Android13關(guān)于獲取外部存儲(chǔ)文件的相關(guān)問(wèn)題及解決方案記錄

    Android13關(guān)于獲取外部存儲(chǔ)文件的相關(guān)問(wèn)題及解決方案記錄

    ? Android的學(xué)習(xí)路上... 測(cè)試設(shè)備:vivo X90s 安卓版本: Android13 開發(fā)環(huán)境:AndroidStudio?Flamingo SDK:33 最近我在Android13的環(huán)境下嘗試寫一個(gè) 文件選擇器 ,以便日后的開發(fā)使用。但是我們知道,從Android13 (API33) 開始,外部存儲(chǔ)權(quán)限發(fā)生了變化,要想讀取外部存儲(chǔ)文件,使用原來(lái)的權(quán)

    2024年01月15日
    瀏覽(42)
  • Android app保活(前臺(tái)服務(wù))

    國(guó)內(nèi)廠商定制,除非廠商給app白名單,否則只能用戶手動(dòng)添加白名單(應(yīng)用自啟和后臺(tái)運(yùn)行),才能通過(guò)前臺(tái)服務(wù)實(shí)現(xiàn)app保活。 這里介紹前臺(tái)服務(wù)相關(guān)實(shí)現(xiàn)方式。 開啟服務(wù): 服務(wù): 清單文件

    2024年02月09日
    瀏覽(22)
  • 關(guān)于安卓13中Android/data目錄下的文件夾只能查看無(wú)法進(jìn)行刪改的問(wèn)題

    因?yàn)樯?jí)了安卓13,然后有個(gè)app需要恢復(fù)數(shù)據(jù),打算和以前一樣直接刪除Android/data下對(duì)應(yīng)目錄再添加,結(jié)果不行,以下是結(jié)合網(wǎng)上以及自己手機(jī)情況來(lái)做的一種解決方案。 準(zhǔn)備: 待恢復(fù)app(包名com.test.ai) 其他app(包名com.other.ai,這個(gè)app當(dāng)做臨時(shí)變量就行,隨便任意app,且知

    2024年02月09日
    瀏覽(30)
  • Android 11/12 app-lint 系統(tǒng)Update-API時(shí)Lint檢查問(wèn)題

    Android 11/12 app-lint 系統(tǒng)Update-API時(shí)Lint檢查問(wèn)題

    這種方式你可以其他博客也有 但是要每個(gè)類和方法都加上 @SuppressLint 太麻煩了 我才不要這樣呢 1. 打開 frameworks/base/Android.bp 文件 2. 搜索找到這個(gè)字段 metalava_framework_docs_args 3. 然后在最后面添加?? --api-lint-ignore-prefix xxxx 蕪湖 成功啦

    2024年02月11日
    瀏覽(33)
  • 關(guān)于在Android 11系統(tǒng)手機(jī)上請(qǐng)求READ_PHONE_STATE權(quán)限的問(wèn)題

    起因是因?yàn)閎ugly報(bào)錯(cuò): 上網(wǎng)查了下,原來(lái)在Android11及以上機(jī)型上調(diào)用telephonyManager.getNetworkType()需要READ_PHONE_STATE權(quán)限,于是我就在應(yīng)用啟動(dòng)時(shí)加上了申請(qǐng)?jiān)摍?quán)限的代碼,并且在調(diào)用getNetworkType()方法的地方加了判斷,如果系統(tǒng)版本大于等于11并且沒有被授予READ_PHONE_STATE權(quán)限,就

    2024年02月12日
    瀏覽(43)
  • MIUI14+安卓13 Root教程 小米10 小米11 小米12 小米13 紅米

    1. 確保手機(jī)已完成 BL 解鎖。這里來(lái)申請(qǐng)解鎖 2. 手機(jī)下載并且安裝Magisk 下載地址 3. 去下載當(dāng)前版本刷機(jī)包 小米 10 (umi) 國(guó)行版 線刷、卡刷包 ? 網(wǎng)頁(yè)上ctrl+f 搜索 miui14,直接找到最新版,目前版本是: ? V14.0.2.0.TJBCNXM MIUI14 13.0 ? miui_UMI_V14.0.2.0.TJBCNXM_6d38dfc521_13.0.zip | 下載 4.把

    2023年04月16日
    瀏覽(27)
  • 【每日刷題】動(dòng)態(tài)規(guī)劃-代碼隨想錄動(dòng)規(guī)-11、12、13

    【每日刷題】動(dòng)態(tài)規(guī)劃-代碼隨想錄動(dòng)規(guī)-11、12、13

    問(wèn)題背景 : 有若干個(gè)物品對(duì)應(yīng)各自的體積和價(jià)值,有一個(gè)容量確定的背包,有選擇的將物品裝進(jìn)背包里,求可放進(jìn)背包的最大價(jià)值。 思路: 定義dp數(shù)組: dp[i][j]的含義:從下標(biāo)為[0-i]的物品里任意取,放進(jìn)容量為j的背包,價(jià)值總和最大是多少。 dp[i][j]遞推公式: 不放物品

    2024年02月22日
    瀏覽(23)
  • 高通 Android 12/13獲取IMIE號(hào)

    高通 Android 12/13獲取IMIE號(hào)

    1、由于我們工廠smt需要顯示imei號(hào),因此需要 2、查閱相關(guān)資料Android O(之后)Android 10之后進(jìn)行限制 ?3、通過(guò)反射獲取 imei號(hào)? 4、通過(guò) TelephonyManager 中g(shù)etImei()方法獲取 5、結(jié)果 獲取imei號(hào) 如下圖所示? ? ?6、到這里基本結(jié)束了,轉(zhuǎn)載請(qǐng)注明出處,謝謝 7、記得添加系統(tǒng)權(quán)限 否則

    2024年02月11日
    瀏覽(23)
  • C Primer Plus(第六版)11.13 編程練習(xí) 第12題

    /* 編寫一個(gè)程序,讀取輸入,直至讀到EOF,報(bào)告讀入的單詞數(shù)、大寫字母數(shù)、小寫字母數(shù)、標(biāo)點(diǎn) 符號(hào)數(shù)和數(shù)字字符數(shù)。使用ctype.h頭文件中的函數(shù)。 */ //測(cè)試字符串? //ajskm,dl kdAj,.lfj sjkdl ?sdk12lfj !.,fkdj.,.lssd.1a //(ajskm),(dl) (kdAj),.(lfj) (sjkdl) ?(sdk)12(lfj) !.,(fkdj).,.(lssd).1(a) #includestdi

    2024年02月02日
    瀏覽(28)
  • 高通Android 12/13 默認(rèn)應(yīng)用程序授予權(quán)限

    1、一提到權(quán)限很多Android開發(fā)者都會(huì)想到 比如撥打電話 讀取手機(jī)通訊錄 定位 這些都是需要申請(qǐng)權(quán)限,Google Android 6.0之后(sdk 23) 需要app動(dòng)態(tài)申請(qǐng)權(quán)限 或者權(quán)限組 2、我這里打個(gè)比方 比如需要在fm應(yīng)用 默認(rèn)打開mic權(quán)限? 3、我們需要知道這個(gè)默認(rèn)應(yīng)用程序的包名 (例如 xxx.

    2024年02月01日
    瀏覽(31)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包