android - 无法从 Android 10 上的 BroadcastReceiver 开始 Activity

标签 android broadcastreceiver android-pendingintent start-activity android-10.0

我昨晚将我的操作系统版本更新为 android 10,从那时起广播接收器内的 startActivity 函数什么也没做。这就是我尝试根据 CommonsWare 的回答开始 Activity 的方式:

Intent i = new Intent(context, AlarmNotificationActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { // This is at least android 10...

                Log.d("Debug", "This is android 10");
                // Start the alert via full-screen intent.
                PendingIntent startAlarmPendingIntent = PendingIntent.getBroadcast(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
                String CHANNEL_ID = "my_channel_02";
                NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
                        context.getString(R.string.notification_channel_name_second),
                        NotificationManager.IMPORTANCE_HIGH);
                NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                notificationManager.createNotificationChannel(channel);
                NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
                        .setContentTitle("Um, hi!")
                        .setAutoCancel(true)
                        .setPriority(NotificationCompat.PRIORITY_HIGH)
                        .setFullScreenIntent(startAlarmPendingIntent, true);
                Log.d("Debug", "Try to load screen");
                notificationManager.notify(0, builder.build());

            }

日志显示我正在执行通知命令,但没有任何 react 。我要求 list 上的 USE_FULL_SCREEN_INTENT 权限,所以我应该能够使用全屏 Intent 。
由于这个问题,我的应用程序现在没用了。有谁知道如何解决它?

最佳答案

Android 10 对后台 Activity 启动的限制大约在六个月前宣布。您可以在 the documentation 中了解更多信息.

使用具有关联全屏的高优先级通知 Intent , 反而。见 the documentation . This sample app通过使用 WorkManager 来证明这一点触发需要提醒用户的后台事件。在那里,我使用高优先级通知而不是直接启动 Activity :

val pi = PendingIntent.getActivity(
  appContext,
  0,
  Intent(appContext, MainActivity::class.java),
  PendingIntent.FLAG_UPDATE_CURRENT
)

val builder = NotificationCompat.Builder(appContext, CHANNEL_WHATEVER)
  .setSmallIcon(R.drawable.ic_notification)
  .setContentTitle("Um, hi!")
  .setAutoCancel(true)
  .setPriority(NotificationCompat.PRIORITY_HIGH)
  .setFullScreenIntent(pi, true)

val mgr = appContext.getSystemService(NotificationManager::class.java)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
  && mgr.getNotificationChannel(CHANNEL_WHATEVER) == null
) {
  mgr.createNotificationChannel(
    NotificationChannel(
      CHANNEL_WHATEVER,
      "Whatever",
      NotificationManager.IMPORTANCE_HIGH
    )
  )
}

mgr.notify(NOTIF_ID, builder.build())

关于android - 无法从 Android 10 上的 BroadcastReceiver 开始 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57833208/

相关文章:

android - 将参数传递给 BroadcastReceiver

android - 无法获得ACTION_PACKAGE_REMOVED广播-Android 10/设置

android - 获取 'java.lang.IllegalArgumentException: Can' t 在运行 android 2.3.3 的设备中使用 FLAG_RECEIVER_BOOT_UPGRADE

android - Phonegap Android 应用程序以编程方式触发更新

android - 安排发布到 Google Play Android Market

android - 整个应用程序中的广播接收器

java - Android AlarmManager.cancel() 实际上并没有取消闹钟/PendingIntent

android - 通知操作按钮不会触发未决 Intent

android - 访问 xml 中的整数资源

Android 使用带有 onContentChanged() 的自定义 CursorAdapter