Android 屏幕关闭时从广播接收器打开活动

Android 屏幕关闭时从广播接收器打开活动,android,broadcastreceiver,Android,Broadcastreceiver,当应用程序运行且设备锁定时,我可以启动活动。但当应用程序处于后台且设备被锁定时,即使我在BroadcastReceiver类中获得控件,也无法启动活动。这是我的意向电话 context.startActivity(new Intent(context, ReceiveCallActivity.class) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) .setAction(Inte

当应用程序运行且设备锁定时,我可以启动活动。但当应用程序处于后台且设备被锁定时,即使我在BroadcastReceiver类中获得控件,也无法启动活动。这是我的意向电话

context.startActivity(new Intent(context, ReceiveCallActivity.class)
                    .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                    .setAction(Intent.ACTION_ANSWER)
                    .putExtra("title", intent.getStringExtra("title"))
                    .putExtra("action", intent.getStringExtra("action")));
活动清单

<activity
            android:name=".ReceiveCallActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:excludeFromRecents="true"
            android:launchMode="singleTop"
            android:showOnLockScreen="true"
            android:showWhenLocked="true"
            android:turnScreenOn="true"
            android:windowSoftInputMode="adjustPan|stateHidden" />
setShowWhenLocked(true)和&setTurnScreenOn(true)
有助于打开应用程序,即使设备已锁定,但应用程序必须位于前台才能打开

PS:在所有情况下,我都在广播接收器中获得控制权


TIA

我正在检查设置中授予Skype的不同权限,注意到“在锁屏上显示”已启用,而我的应用程序已禁用相同的权限。启用后,BroadcastReceiver能够在所有场景中打开活动。我读到这是Xiamoi设备的一个问题(我使用的是Note5 Pro)

编辑

对于Android 10,需要在清单中添加
使用全屏\u意图
权限。 然后,当屏幕被锁定时,将调用
NotificationCompat.Builder
上设置为
FullScreenIntent
的PendingEvent

我的通知代码:

private void showCallNotification(Map<String, String> dataMap) {

//CREATING pendingIntent
...
...
...

        PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(this, 2, fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        PendingIntent cancelPendingIntent = PendingIntent.getBroadcast(this, 1, cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        RemoteViews notificationLayout = new RemoteViews(getPackageName(), R.layout.notification_small);
        notificationLayout.setTextViewText(R.id.tvTitle, dataMap.get("sender"));
        notificationLayout.setTextViewText(R.id.tvContent, getString(R.string.incoming_call));
        notificationLayout.setOnClickPendingIntent(R.id.tvAccept, pendingIntent);
        notificationLayout.setOnClickPendingIntent(R.id.tvDecline, cancelPendingIntent);

        RemoteViews notificationLayoutExpanded = new RemoteViews(getPackageName(), R.layout.notification_large);
        notificationLayoutExpanded.setTextViewText(R.id.tvTitle, dataMap.get("sender"));
        notificationLayoutExpanded.setTextViewText(R.id.tvContent, getString(R.string.incoming_call));
        notificationLayoutExpanded.setOnClickPendingIntent(R.id.btAccept, pendingIntent);
        notificationLayoutExpanded.setOnClickPendingIntent(R.id.btDecline, cancelPendingIntent);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, AppConstants.CALL_CHANNEL_ID)
                .setSmallIcon(R.drawable.ic_notification)
                .setContentTitle(dataMap.get("sender"))
                .setContentText(getString(R.string.incoming_call))
                .setAutoCancel(true)
                .setTimeoutAfter(CALL_DISMISS_TIME)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .setCustomBigContentView(notificationLayout)
                .setCustomContentView(notificationLayoutExpanded)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setContentIntent(fullScreenPendingIntent)
                .setFullScreenIntent(fullScreenPendingIntent, true);

        if (Build.VERSION.SDK_INT < 26) {
            builder.setPriority(NotificationCompat.PRIORITY_MAX);
        }

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createCallNotificationChannel();
        }

        notificationManager.notify(notificationId, builder.build());
}

不,你不能那样做。你可以做一件事,在ReceiveCallActivity中运行的代码可以在BroadcastReceiver中调用它。ReceiveCallActivity是一个带有UI的呼叫-应答拒绝屏幕。顺便说一句,Skype应用程序在所有情况下都会在收到呼叫时打开。@Shahal,1。您正在尝试的版本2。我正在检查设置中赋予Skype的不同权限,注意到“在锁定屏幕上显示”已启用,而我的应用程序已禁用相同的权限。启用后,BroadcastReceiver能够在所有场景中打开活动。我读到这是Xiamoi设备的问题(我使用的是Note 5 Pro)。在Android 10上,当应用程序处于后台时,我会用RemoteView显示通知。这应该是注释。这不是answer@RakeshKumar真的很抱歉。你说得对,这在安卓10中没有帮助,但在下面这是可行的。
private void showCallNotification(Map<String, String> dataMap) {

//CREATING pendingIntent
...
...
...

        PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(this, 2, fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        PendingIntent cancelPendingIntent = PendingIntent.getBroadcast(this, 1, cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        RemoteViews notificationLayout = new RemoteViews(getPackageName(), R.layout.notification_small);
        notificationLayout.setTextViewText(R.id.tvTitle, dataMap.get("sender"));
        notificationLayout.setTextViewText(R.id.tvContent, getString(R.string.incoming_call));
        notificationLayout.setOnClickPendingIntent(R.id.tvAccept, pendingIntent);
        notificationLayout.setOnClickPendingIntent(R.id.tvDecline, cancelPendingIntent);

        RemoteViews notificationLayoutExpanded = new RemoteViews(getPackageName(), R.layout.notification_large);
        notificationLayoutExpanded.setTextViewText(R.id.tvTitle, dataMap.get("sender"));
        notificationLayoutExpanded.setTextViewText(R.id.tvContent, getString(R.string.incoming_call));
        notificationLayoutExpanded.setOnClickPendingIntent(R.id.btAccept, pendingIntent);
        notificationLayoutExpanded.setOnClickPendingIntent(R.id.btDecline, cancelPendingIntent);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, AppConstants.CALL_CHANNEL_ID)
                .setSmallIcon(R.drawable.ic_notification)
                .setContentTitle(dataMap.get("sender"))
                .setContentText(getString(R.string.incoming_call))
                .setAutoCancel(true)
                .setTimeoutAfter(CALL_DISMISS_TIME)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .setCustomBigContentView(notificationLayout)
                .setCustomContentView(notificationLayoutExpanded)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setContentIntent(fullScreenPendingIntent)
                .setFullScreenIntent(fullScreenPendingIntent, true);

        if (Build.VERSION.SDK_INT < 26) {
            builder.setPriority(NotificationCompat.PRIORITY_MAX);
        }

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createCallNotificationChannel();
        }

        notificationManager.notify(notificationId, builder.build());
}
if (Build.VERSION.SDK_INT > 28) {
   if (isAppOnForeground(getApplicationContext())) {
       sendBroadcast(remoteMessage);
   } else {
       showCallNotification(dataMap);
   }
} else {
   sendBroadcast(remoteMessage);
}