我昨晚将我的操作系统版本更新为 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/