如何在Android 10中打开活动(传入的VoIP呼叫)

2022-09-01 14:39:40

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

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

我绝对知道的是:

  • 我没有任何运行活动,任务,后堆栈等

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

我需要实现的目标:

  • 该应用程序的FCM服务从我们的服务器接收推送,并应显示来电屏幕(通过锁定屏幕和所有 - 就像Android 9及更低版本一样)

在 android 10 中,如何打开传入 VoIP 呼叫的活动?通过锁屏界面和所有内容,就像普通用户对PHONE应用程序的期望一样。

提前感谢您的任何提示。


答案 1

通过锁定屏幕打开“活动记录”。您可以使用具有“全屏意图”的高通知作为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手机上进行了测试。它工作得很好。但对于xaomi设备。您需要通过以下步骤通过流程启用一些权限:

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

现在,你的应用将在 xaomi 设备上工作。如果您遇到我的解决方案的任何问题,请在此处发表评论。如果可以的话,我会帮助你。


答案 2

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

  • 如果设备已锁定,则调用您的“全屏意图”
  • 否则,显示“提醒”通知

推荐