[英]How to open activity (incoming voip call) in Android 10
在 Android 10 中,對應用程序應用了新的限制。 我們不能再從后台啟動活動。 雖然這對大多數應用程序來說可能沒問題,但對於需要在推送通知到達后顯示來電的 voip 應用程序來說,這是一個致命的打擊。
根據這個https://developer.android.com/guide/components/activities/background-starts有一個可以滿足的條件列表仍然允許打開活動,但我不完全理解(非這里是英語母語)。
我絕對知道的是:
我沒有任何正在運行的活動、任務、后台堆棧等
該應用程序甚至沒有運行
我需要達到的目標:
如何在 android 10 中打開傳入 voip 呼叫的活動? 就像普通用戶對 PHONE 應用程序所期望的那樣,在鎖屏和所有內容中。
提前感謝您的任何提示。
使用具有“全屏意圖”的高優先級通知。 這將:
在鎖定屏幕上打開活動。 您可以使用具有“全屏意圖”的高通知作為 CommonsWare 的答案。 但有關更多詳細信息,您可以嘗試我的解決方案,如下代碼:
創建一個前台服務,然后在 onStartCommand 方法中調用 buildNotification,buildNotification 方法將返回一個通知放入 startForeground 方法參數中。
public class IncomingCallService extends Service { public int onStartCommand(Intent intent, int flags, int startId) { Notification notification = buildNotification(); startForeground(1, notification); return START_NOT_STICKY; } }
在 buildNotification 方法中,我們將創建具有高優先級、調用類別和全屏意圖的通知。
private Notification buildNotification() { Intent fullScreenIntent = new Intent(this, IncomingCallActivity.class); PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(this, 0, fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_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 10 // or higher, you need to request the USE_FULL_SCREEN_INTENT permission in // order for the platform to invoke this notification. .setFullScreenIntent(fullScreenPendingIntent, true); notificationBuilder.setAutoCancel(true); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { notificationManager.createNotificationChannel(new NotificationChannel("123", "123", NotificationManager.IMPORTANCE_HIGH)); notificationBuilder.setChannelId("123"); } Notification incomingCallNotification = notificationBuilder.build(); return incomingCallNotification; }
在 onStartCommand 中,添加一行代碼發送 ACTION_CLOSE_SYSTEM_DIALOGS 廣播動作。 這驗證重要的是啟動全屏掛起意圖。
public int onStartCommand(Intent intent, int flags, int startId) { Notification notification = buildNotification(); startForeground(1, notification); sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)); return START_NOT_STICKY; }
創建要在鎖定屏幕上顯示的全屏活動,然后您需要添加 setShowWhenLocked 和 setTurnScreenOn 以在鎖定屏幕上顯示。 如果沒有,您的活動將顯示在鎖定屏幕后面。 下面是我的樣本。
public class IncomingCallActivity extends AppCompatActivity { protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_explore); setShowWhenLocked(true); setTurnScreenOn(true); getWindow().addFlags( WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON); } }
現在,當您收到來自您的邏輯的呼叫時,您必須啟動 IncomingCallService。
public void startCallService() { Intent intent = new Intent(context, IncomingCallService.class); startForegroundService(intent); }
您必須在清單中聲明活動、服務和一些權限,如下所示:
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" /> <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <application...> <activity android:name=".IncomingCallActivity" /> <service android:name=".IncomingCallService" android:enabled="true" android:exported="true" /> </application>
我在谷歌、三星、vsmart 手機上測試過。 它運作良好。 但是對於小米設備。 您需要通過以下步驟啟用一些權限:
現在您的應用程序將在 xaomi 設備上運行。 如果您對我的解決方案有任何問題,請在此處發表評論。 如果可以的話,我會幫助你。
請通過我的博客 go 了解如何為 OS 10 打開活動以及如何顯示抬頭通知和處理對操作按鈕的點擊。
檢查此鏈接,這將在這里為您提供幫助
或者
您需要請求“繪制其他應用程序”的權限,然后您可以將其設置為以前的版本
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
if (!Settings.canDrawOverlays(this)) {
RequestPermission();
}
}
private void RequestPermission() {
// Check if Android P or higher
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// Show alert dialog to the user saying a separate permission is needed
// Launch the settings activity if the user prefers
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + BuildConfig.APPLICATION_ID));
startActivityForResult(intent,
ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE);
}
}
或者你可以使用我的這個答案
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.