[android q] 从后台启动 Activity 的限制

https://developer.android.com/guide/components/activities/background-starts

https://developer.android.com/training/notify-user/time-sensitive

从Android Q开始android进一步限制了从后台(组件)启动Activity的行为,前台的不受限制。说白了就是用户不知道,你不能随便就蹦出来一个新的界面。想要启动,必须通过“显示通知”的方式来进行,通知用户借助notification来启动新的Activity。

比如

    Intent fullScreenIntent = new Intent(this, CallActivity.class);
    PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(this, 0,
            fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder notificationBuilder =
            new 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 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);

    Notification incomingCallNotification = notificationBuilder.build();

    // Provide a unique integer for the "notificationId" of each notification.
    startForeground(notificationId, incomingCallNotification );

 

你可能感兴趣的:([android q] 从后台启动 Activity 的限制)