27

在 Android 10 中,应用程序应用了新的限制。我们不能再从后台启动活动。虽然这对大多数应用程序来说可能没问题,但对于需要在推送通知到达后显示来电的 voip 应用程序来说,这是一个致命的打击。

根据这个https://developer.android.com/guide/components/activities/background-starts有一个可以满足的条件列表仍然允许打开一个活动,但我不完全理解(非这里是英语母语)。

我绝对知道的是:

  • 我没有任何正在运行的活动、任务、后台堆栈等

  • 该应用程序甚至没有运行

我需要达到的目标:

  • 应用程序的 FCM 服务从我们的服务器接收推送,并显示来电屏幕(锁定屏幕和所有 - 就像它在 android 9 及更低版本中所做的那样)

我该怎么做才能在 android 10 中打开传入 voip 呼叫的活动?就像普通用户对 PHONE 应用程序所期望的那样,在锁屏和所有内容中。

提前感谢您的任何提示。

4

4 回答 4

10

使用具有“全屏意图”的高优先级通知。这将:

  • 如果设备被锁定,请调用您的“全屏意图”
  • 否则,显示“提醒”通知
于 2019-11-12T13:24:53.633 回答
10

在锁定屏幕上打开活动。您可以使用具有“全屏意图”的高通知作为 CommonsWare 的答案。但有关更多详细信息,您可以尝试我的解决方案,如下代码:

  1. 创建一个前台服务,然后在 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;
         }
     }
    
  2. 在 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;
     }
    
  3. 在 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;
     }
    
  4. 创建要在锁定屏幕上显示的全屏活动,然后您需要添加 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);
         }
     }
    
  5. 现在,当您收到来自您的逻辑的呼叫时,您必须启动 IncomingCallService。

     public void startCallService() {
         Intent intent = new Intent(context, IncomingCallService.class);
         startForegroundService(intent);
     }
    
  6. 您必须在清单中声明活动、服务和一些权限,如下所示:

     <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 手机上测试过。它运作良好。但是对于小米设备。您需要通过以下步骤启用一些权限:

  1. 长按到你的应用程序图标
  2. 打开应用信息
  3. 点击“其他权限”项目
  4. 允许在锁定屏幕上显示

现在您的应用程序将在 xaomi 设备上运行。如果您对我的解决方案有任何问题,请在此处发表评论。如果可以的话,我会帮助你。

于 2021-02-05T07:28:54.370 回答
2

请浏览我的博客,了解如何为 OS 10 打开活动,以及如何显示抬头通知和处理对操作按钮的点击。

https://medium.com/@dcostalloyd90/show-incoming-voip-call-notification-and-open-activity-for-android-os-10-5aada2d4c1e4

于 2020-03-16T07:49:42.673 回答
0

检查此链接,这将 在这里为您提供帮助

或者

您需要请求“绘制其他应用程序”的权限,然后您可以将其设置为以前的版本

  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);
        }
     }

或者你可以使用我的这个答案

https://stackoverflow.com/a/63699960/7108113

于 2020-04-09T05:49:01.803 回答