StartActivity does not work when called from BroadcastReceiver

I have a notification, and when I select, it sends Broadcast to BroadcastReceiver using PendingIntent . In onReceive I am launching a new Activity .

However, if I remove my application from the last open applications (or the notification sits in a draw for a long time), this scenario occurs:

When I have several notifications in the mailbox, the first one opens perfectly. After you press the second one, my onCreate() and my onResume() are called, and as if startActivity() doesn't work at all. If I add the Intent.FLAG_ACTIVITY_SINGLE_TOP flag, then onNewIntent is onNewIntent .

 notificationIntent = new Intent(); notificationIntent.setAction(AppConstants.ACTION_ACTIVITY); notificationIntent.putExtra("key", value); int requestID = (int) System.currentTimeMillis(); mBuilder.setContentIntent(PendingIntent .getBroadcast(context, requestID, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT)); 

Onreceive

  Intent intent = new Intent(context, Activity.class); intent.putExtra("key", value); //IF I ADD FLAG_ACTIVITY_SINGLE_TOP IT WORKS intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); 
+6
source share
4 answers

Try using flags as follows for intent.

 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK); 

And for the pending intent, use PendingIntent.FLAG_UPDATE_CURRENT

+12
source

to get started with BroadcastReciever you must add the flag FLAG_ACTIVITY_NEW_TASK

  Intent imap =new Intent(context,MainActivity.class); imap.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(imap); 
+3
source

Activity can be triggered from a broadcast receiver, even if it is not a launcher. This should not be a problem.

Try passing a unique requestid,

 notificationIntent = new Intent(); notificationIntent.setAction(AppConstants.ACTION_ACTIVITY); notificationIntent.putExtra("key", value); mBuilder.setContentIntent(PendingIntent.getBroadcast(context, value, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT)); 

See this question .

+1
source

For someone who will be looking for a similar question. Starting with Android Q, it is limited to trigger any action when the application is in the background. To get started, you need to display a notification using PRIORITY_HIGH and set the full-screen intent . So you should have something like this:

 //Create your intent as usual Intent intent = new Intent(context, Activity.class); intent.putExtra("key", value); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // Build a pending intent PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); //Notification builder NotificationCompat.Builder notifyBuilder = new NotificationCompat.Builder(context, CHANNEL_ID) .setSmallIcon(R.drawable.ic_notification) .setContentTitle(title) .setPriority(PRIORITY_HIGH) .setFullScreenIntent(pIntent, true /*isHighPriority*/); NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); nManager.notify(10, notifyBuilder.build()) 

This will display a notification and start your activity immediately. And in your onStart() activity, you can reject the notification, as shown below:

 NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); nManager.cancel(10); 

NOTE : Remember to initiate the channel. Starting with Android O, this is a must

0
source

Source: https://habr.com/ru/post/1213175/


All Articles