简体   繁体   English

屏幕关闭时从 BroadcastReceiver 打开活动

[英]Open Activity from BroadcastReceiver when screen off

When app is running and device is locked I'm able to start the activity.当应用程序正在运行并且设备被锁定时,我可以开始活动。 But when app is in background and device is locked not able to start the activity even though I'm getting the control in BroadcastReceiver class.但是当应用程序处于后台并且设备被锁定时,即使我在 BroadcastReceiver 类中获得控件也无法启动活动。 This is my intent call.这是我的意图电话。

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

Manifest of Activity活动清单

<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" />

ReceiveCallActivity.class ReceiveCallActivity.class

 @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
            setShowWhenLocked(true);
            setTurnScreenOn(true);
        } else {
            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);
        }
        setContentView(R.layout.receive_call_activity);
        ...
        ...
}

setShowWhenLocked(true) && setTurnScreenOn(true) helps to open app even if device is locked but app has to be in foreground for that.即使设备被锁定, setShowWhenLocked(true) && setTurnScreenOn(true)有助于打开应用程序,但应用程序必须为此处于前台。

PS: I'm getting the control in BroadcastReceiver in all scenarios. PS:我在所有场景中都获得了 BroadcastReceiver 的控制权。

TIA TIA

I was checking different permissions given to Skype from Settings, and noticed 'Show on Lock Screen' is enabled while the same was disabled for my App.我正在检查从“设置”中授予 Skype 的不同权限,并注意到“在锁定屏幕上显示”已启用,而我的应用程序已禁用该权限。 On enabling it, BroadcastReceiver is able to open Activity in all scenarios.启用它后,BroadcastReceiver 能够在所有场景中打开 Activity。 I read it's a issue with Xiamoi devices(I'm using Note 5 Pro).我读到这是小米设备的问题(我使用的是 Note 5 Pro)。

EDIT编辑

For Android 10 need to add USE_FULL_SCREEN_INTENT permission in manifest.对于 Android 10,需要在清单中添加USE_FULL_SCREEN_INTENT权限。 Then when the screen is locked, PendingIntent set as FullScreenIntent on NotificationCompat.Builder will be called.然后当屏幕被锁定时,将调用NotificationCompat.Builder上设置为FullScreenIntent PendingIntent。

My Notification code:我的通知代码:

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

And i call it like this我这样称呼它

onMessageReceived() onMessageReceived()

if (Build.VERSION.SDK_INT > 28) {
   if (isAppOnForeground(getApplicationContext())) {
       sendBroadcast(remoteMessage);
   } else {
       showCallNotification(dataMap);
   }
} else {
   sendBroadcast(remoteMessage);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM