无法在安卓 10上从BroadcastReceiver启动活动
昨晚我把我的操作系统版本升级到了安卓10,从那以后,广播接收器内的startActivity功能就什么都没做了。以下是我如何根据Commonware的答案开始活动:
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 安卓 10...
Log.d("Debug", "This is 安卓 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());
}
日志显示我正在使用notify命令,但什么也没发生。我正在申请清单上的使用全屏意图许可,这样我就可以使用全屏意图了。 因为这个问题,我的应用程序现在没用了。有人知道怎么解决吗
# 1 楼答案
安卓10对后台活动启动的限制大约在六个月前宣布。你可以在the documentation中阅读更多关于它的信息
使用高优先级通知,并使用相关的全屏
Intent
。见the documentationThis sample app通过使用WorkManager
触发需要提醒用户的后台事件来演示这一点。在那里,我使用高优先级通知,而不是直接启动活动:# 2 楼答案
安卓10对后台活动启动的限制大约在六个月前宣布。你可以在documentation中阅读更多关于它的信息
因此,您需要有一个高级通知,当用户单击该通知时,您的活动将被打开