日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android Q 限制后台启动Activity

發布時間:2023/12/20 Android 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android Q 限制后台启动Activity 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

描述

Android Q限制在沒有用戶交互的情況下加載Activity。這一變化可以最大限度的減少對用戶的打擾,保持用戶對屏幕上所顯示內容的可控性。
運行在Android Q上的APP僅在以下一種或多種情況下可運行Activity:

  • APP存在一個可視的窗口,例如一個處于前臺的Activity
  • 另外一個處于前臺的APP發送一個屬于當前APP的PendingIntent。例如Custom Tabs provider發送一個menu item pending intent。
  • 系統發送一個PendingIntent,例如點擊一個通知。
  • 系統給APP發送一個廣播,如SECRET_CODE_ACTION。
  • 注:APP啟動一個前臺運行的Service并不代表該APP處于前臺運行狀態。

    這一行為變化影響所有運行在Android Q上的app,包括target是Android 9或更低的APP。如果你的APP target是Android 9及以下,并且之前運行在Android 9或更低版本的設備上,那么在該設備升級到Android Q以后,該APP也會受到影響。

    警告信息

    在Android Q Beta 1版本中,如果你的APP試圖從后臺啟動一個Activity,平臺允許加載Activity,但是會在控制臺輸出警告信息,并且會給用于Toast提示:
    This background activity start from package-name will be blocked in future Q builds.

    創建通知

    谷歌建議使用通知的方式解決這一問題
    絕大多數情況下,運行在后臺的APP應該創建一個通知,給用戶提供一些必要的信息,而不應該直接從后臺啟動Activity。
    某些特殊情況下,你的APP可能需要立即引起用戶的注意,例如鬧鐘響鈴或來電提醒。你可以通過以下方式來達到提醒用戶的目的:

  • 創建一個高級別的通知
    確保你創建的通知的標題和消息可以準確表達你的意圖。你也可以提供一個FullScreenIntent,示例代碼如下:
  • val fullScreenIntent = Intent(this, CallActivity::class.java) val fullScreenPendingIntent = PendingIntent.getActivity(this, 0,fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT)val notificationBuilder = NotificationCompat.Builder(this, CHANNEL_ID).setSmallIcon(R.drawable.notification_icon).setContentTitle("Incoming call").setContentText("(919) 555-1234").setPriority(NotificationCompat.PRIORITY_HIGH).setCategory(NotificationCompat.CATEGORY_CALL)// Use a full-screen intent only for the highest-priority alerts where you// have an associated activity that you would like to launch after the user// interacts with the notification. Also, if your app targets Android Q, you// need to request the USE_FULL_SCREEN_INTENT permission in order for the// platform to invoke this notification..setFullScreenIntent(fullScreenPendingIntent, true)val incomingCallNotification = notificationBuilder.build()
  • 顯示通知給用戶
    當顯示通知給用戶時,用戶可以根據當前的情況選擇接受或者取消該通知。例如,用戶可以選擇接電話,也可以掛掉電話。
    如果你的通知是連續不間斷的,例如來電提醒,可以給該通知關聯一個foreground service:
  • // Provide a unique integer for the "notificationId" of each notification. startForeground(notificationId, notification)

    當用戶正在使用設備時,系統UI會顯示一個heads-up notification取代你的full-screen intent

    使用通知的優點

    • 當用戶正在使用設備時,系統顯示一個heads-up notification,用戶可以選擇根據當前情況有選擇性的處理這個通知。
    • 遵守了不打擾用戶的規則(Do Not Disturb)
    • 當設備屏幕關閉時,full-screen intent可以立即顯示出來
    • 在Settings界面,用戶可以看到最近有哪些App發送過通知,包括從特殊通知渠道發送的。在這個界面,用戶可以控制通知是否顯示。

    在Android Q Beta1版本中,上述行為變化還沒有生效。你可以通過開發者選擇來模擬這一變化:

    • Settings -> Developer options,取消選擇 Allow background activity starts
    • 或者使用命令行:
    adb shell settings put global background_activity_starts_enabled 0

    參考鏈接:https://developer.android.google.cn/preview/privacy/background-activity-starts

    總結

    以上是生活随笔為你收集整理的Android Q 限制后台启动Activity的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。