Can';t在android 10上从BroadcastReceiver启动活动
昨晚我把我的操作系统版本升级到了安卓10,从那以后,广播接收器内的startActivity功能就什么都没做了。以下是我如何根据Commonware的答案开始活动:Can';t在android 10上从BroadcastReceiver启动活动,android,broadcastreceiver,android-pendingintent,start-activity,android-10.0,Android,Broadcastreceiver,Android Pendingintent,Start Activity,Android 10.0,昨晚我把我的操作系统版本升级到了安卓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
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());
}
日志显示我正在使用notify命令,但什么也没发生。我请求在清单上使用全屏意图权限,以便我能够使用全屏意图。
因为这个问题,我的应用程序现在没用了。有人知道怎么解决吗 安卓10对后台活动启动的限制大约在六个月前宣布。您可以在中阅读更多关于它的信息 改为使用高优先级通知和相关的全屏
意图。看见通过使用WorkManager
触发需要提醒用户的后台事件,演示了这一点。在这里,我使用高优先级通知,而不是直接启动活动:
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())
安卓10对后台活动启动的限制大约在六个月前宣布。您可以在中阅读更多关于它的信息
改为使用高优先级通知和相关的全屏意图。看见通过使用WorkManager
触发需要提醒用户的后台事件,演示了这一点。在这里,我使用高优先级通知,而不是直接启动活动:
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())
安卓10对后台活动启动的限制大约在六个月前宣布。你可以在网站上阅读更多关于它的信息
因此,您需要有一个高级通知,当用户单击该通知时,您的活动将被打开
public class UIExampleReceiver extends BroadcastReceiver {
public static final String TAG_NOTIFICATION = "NOTIFICATION_MESSAGE";
public static final String CHANNEL_ID = "channel_1111";
public static final int NOTIFICATION_ID = 111111;
private static final String TAG = "Receiver";
@Override
public void onReceive(Context context, Intent intent) {
try {
// If android 10 or higher
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P)
{
startActivityNotification(context,NOTIFICATION_ID,context.getResources().getString(R.string.open_app), context.getResources().getString(R.string.click_app));
}
else
{
// If lower than Android 10, we use the normal method ever.
Intent activity = new Intent(context, ExampleActivity.class);
activity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(activity);
}
} catch (Exception e)
{
Log.d(TAG,e.getMessage()+"");
}
}
// notification method to support opening activities on Android 10
public static void startActivityNotification(Context context, int notificationID,
String title, String message) {
NotificationManager mNotificationManager =
(NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
//Create GPSNotification builder
NotificationCompat.Builder mBuilder;
//Initialise ContentIntent
Intent ContentIntent = new Intent(context, ExampleActivity.class);
ContentIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent ContentPendingIntent = PendingIntent.getActivity(context,
0,
ContentIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title)
.setContentText(message)
.setColor(context.getResources().getColor(R.color.colorPrimaryDark))
.setAutoCancel(true)
.setContentIntent(ContentPendingIntent)
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID,
"Activity Opening Notification",
NotificationManager.IMPORTANCE_HIGH);
mChannel.enableLights(true);
mChannel.enableVibration(true);
mChannel.setDescription("Activity opening notification");
mBuilder.setChannelId(CHANNEL_ID);
Objects.requireNonNull(mNotificationManager).createNotificationChannel(mChannel);
}
Objects.requireNonNull(mNotificationManager).notify(TAG_NOTIFICATION,notificationID,
mBuilder.build());
}
}
安卓10对后台活动启动的限制大约在六个月前宣布。你可以在网站上阅读更多关于它的信息
因此,您需要有一个高级通知,当用户单击该通知时,您的活动将被打开
public class UIExampleReceiver extends BroadcastReceiver {
public static final String TAG_NOTIFICATION = "NOTIFICATION_MESSAGE";
public static final String CHANNEL_ID = "channel_1111";
public static final int NOTIFICATION_ID = 111111;
private static final String TAG = "Receiver";
@Override
public void onReceive(Context context, Intent intent) {
try {
// If android 10 or higher
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P)
{
startActivityNotification(context,NOTIFICATION_ID,context.getResources().getString(R.string.open_app), context.getResources().getString(R.string.click_app));
}
else
{
// If lower than Android 10, we use the normal method ever.
Intent activity = new Intent(context, ExampleActivity.class);
activity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(activity);
}
} catch (Exception e)
{
Log.d(TAG,e.getMessage()+"");
}
}
// notification method to support opening activities on Android 10
public static void startActivityNotification(Context context, int notificationID,
String title, String message) {
NotificationManager mNotificationManager =
(NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
//Create GPSNotification builder
NotificationCompat.Builder mBuilder;
//Initialise ContentIntent
Intent ContentIntent = new Intent(context, ExampleActivity.class);
ContentIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent ContentPendingIntent = PendingIntent.getActivity(context,
0,
ContentIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title)
.setContentText(message)
.setColor(context.getResources().getColor(R.color.colorPrimaryDark))
.setAutoCancel(true)
.setContentIntent(ContentPendingIntent)
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID,
"Activity Opening Notification",
NotificationManager.IMPORTANCE_HIGH);
mChannel.enableLights(true);
mChannel.enableVibration(true);
mChannel.setDescription("Activity opening notification");
mBuilder.setChannelId(CHANNEL_ID);
Objects.requireNonNull(mNotificationManager).createNotificationChannel(mChannel);
}
Objects.requireNonNull(mNotificationManager).notify(TAG_NOTIFICATION,notificationID,
mBuilder.build());
}
}
您可以在android 10中使用SYSTEM_ALERT_WINDOW强制启动活动窗口,请参阅以下设置:
您可以将用户意图更改为android旧版本
public class OnBootReceiver extends BroadcastReceiver {
private static final String TAG = OnBootReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
try {
Intent activity = new Intent(context, MainActivity.class);
activity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(activity);
} catch (Exception e){
Log.d(TAG,e.getMessage()+"");
}
}
}
您可以在android 10中使用SYSTEM_ALERT_WINDOW强制启动活动窗口,请参阅以下设置:
您可以将用户意图更改为android旧版本
public class OnBootReceiver extends BroadcastReceiver {
private static final String TAG = OnBootReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
try {
Intent activity = new Intent(context, MainActivity.class);
activity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(activity);
} catch (Exception e){
Log.d(TAG,e.getMessage()+"");
}
}
}
@SimpleUXApps:如果您试图启动活动,请不要使用PendingEvent.getBroadcast()
。使用pendingent.getActivity()
@steller:“我正在制作一个闹钟应用程序,我需要显示一个闹钟屏幕”--闹钟应用程序多年来一直使用全屏高优先级通知。这是一本书。如果用户的设备被锁定,您的活动将立即显示,因为Android知道显示活动是安全的。而且,在其他情况下,它不会从用户使用它的目的接管屏幕,因为这可能会对用户造成伤害。@steller:“我对此感到有点困惑,因为我的pixel 2和android 10处于锁定状态,并且意图没有激发”——我猜您没有实现具有全屏意图的高优先级通知。这就是我所指的。“你是否有一个适当的链接来展示你所谈论的内容的例子?”——这在答案和中都有涉及。@steller:是的。您可以为此使用一对操作,以及相关图标。例如,引发具有全屏意图和操作的通知。这个特定的示例并不实用,但它展示了API是如何工作的。@amp:至少在三星Galaxy S9上,我需要点击锁屏上的通知,然后滑动,以便显示活动。设备制造商可能正在修补机械装置。但是,对于如何改进该过程,我没有任何建议。@SimpleUXApps:如果您试图启动一个活动,请不要使用PendingEvent.getBroadcast()
。使用pendingent.getActivity()
@steller:“我正在制作一个闹钟应用程序,我需要显示一个闹钟屏幕”--闹钟应用程序多年来一直使用全屏高优先级通知。这是一本书。如果用户的设备被锁定,您的活动将立即显示,因为Android知道显示活动是安全的。而且,在其他情况下,它不会从用户使用它的目的接管屏幕,因为这可能会对用户造成伤害。@steller:“我对此感到有点困惑,因为我的pixel 2和android 10处于锁定状态,并且意图没有激发”——我猜您没有实现具有全屏意图的高优先级通知。这就是我所指的。“你是否有一个适当的链接来展示你所谈论的内容的例子?”——这在答案和中都有涉及。@steller:是的。您可以为此使用一对操作,以及相关图标。例如,引发具有全屏意图和操作的通知。这个特定的示例并不实用,但它展示了API是如何工作的。@amp:至少在三星Galaxy S9上,我需要点击锁屏上的通知,然后滑动,以便显示活动。设备制造商可能正在修补机械装置。但是,我没有任何关于如何改进流程的建议。即使应用程序被关闭并且屏幕被锁定,这也会打开活动。谢谢。这会在应用程序被关闭且屏幕被锁定时打开活动。谢谢