How can you determine if your activity has been suspended due to the PendingIntent you created?

If I have an Activity that is in the foreground and I switch to another Activity in my application, I can detect this by setting a flag when you start the transfer and wiping it when you move to a new action. This allows me to distinguish between onPause due to an internal (flag set) or external (flag not set) event.

However, I am having trouble with this for the PendingIntents built into Notifications. Is it possible to detect that my activity is on the application side, because they selected the notification that I created in the notification panel? Is there some kind of notification listener that I can use that will run before , and a notification is triggered waiting for the intent, which is onPauses my Activity?

I understand that this is somewhat confusing, so I made a skeleton project that demonstrates the problem:

package com.example.notificationtest; import android.os.Bundle; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.util.Log; import android.view.View; public class MainActivity extends Activity { private static final int INCOMING_NOTIFICATION_ID = 1; private static final String TAG = "NotificationTest"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public void onPause() { super.onPause(); Log.e(TAG,"onPause"); } @Override public void onResume() { super.onResume(); Log.e(TAG,"onResume"); } public void onTransitionButtonClick(View view) { Intent intent = new Intent(this, MainActivity.class); Log.e(TAG,"About to start activity: onPause will be invoked."); startActivity(intent); } public void onNotificationButtonClick(View view) { Intent intent = new Intent(this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); Notification notification = new Notification(android.R.drawable.alert_dark_frame, "Alert!", System.currentTimeMillis()); notification.defaults |= Notification.DEFAULT_SOUND; notification.defaults |= Notification.DEFAULT_VIBRATE; notification.defaults |= Notification.DEFAULT_LIGHTS; notification.flags |= Notification.FLAG_AUTO_CANCEL; notification.flags |= Notification.FLAG_INSISTENT; notification.setLatestEventInfo(this, "Click to open", "Click to open", pendingIntent); // Show the alert. final NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(INCOMING_NOTIFICATION_ID, notification); } } 

With Activity_main.xml:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/notify_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="@dimen/padding_medium" android:text="Make a notification" android:onClick="onNotificationButtonClick" tools:context=".MainActivity" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/notify_button" android:padding="@dimen/padding_medium" android:text="Normal transition" android:onClick="onTransitionButtonClick" tools:context=".MainActivity" /> </RelativeLayout> 

If you select the Normal Transition button, the log prints "About to get started: onPause will be called." until onPause.

If you select the “Make a Notification” button and then drag the notification bar and click on the notification, I want to receive information about this receipt before onPause, so that I can insert the same line “About launching activity: onPause will be called.”. How can i do this?

+4
source share
2 answers

In addition to the intention to get started, PendingIntent can also hold the intention to “start” broadcasting .

you can use a custom broadcast like a trigger for notification, and then implement BroadcastReceiver , which will be registered in this broadcast, and will show everything you want, and only after that will start the required activity.

 Intent intent = new Intent("my.custom.broadcast.action"); pendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0); 

the receiver will look something like this:

 private BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { //do whatever you want // if this code is being executed - it a sign that the user clicked on the notification... Intent intent = new Intent(this, MainActivity.class); Log.e(TAG,"About to start activity: onPause will be invoked."); startActivity(intent); } }; 

don't forget to register mReceiver when calling onCreate() and unregister onDestroy()

 registerReceiver(mReceiver , new IntentFilter("my.custom.broadcast.action")); unregisterReceiver(mReceiver); 

in this approach, you can precisely control what happens when a user clicks on a notification. as you can see, it’s only possible to send a broadcast when you click on a notification. no one said you should start practicing at all.

+2
source

Let me start by translating your question to another (just for clarity): how can I execute my execution code before it is paused, because the action associated with my notification will be displayed (which I can control)

Provided that the translation is good enough for the answer:

Are you sure this approach will be the solution? Since there are many cases where your activity will be paused and the user will display your notification activity “almost immediately after”, while there will still be intermediate steps (for example, when other notifications are viewed first). I assume that this technically means that you should still execute the same code there, and at the same time you cannot have any indication that your notification will be viewed "in the very near future."

So, for a suitable answer, I believe that you need to think a little outside the field, depending on the nature of the code that you intend to execute, and the motivation for it.

So, if you update your question in this regard and notify me, I will be glad that I will look a second time.

Update after updating the question:

You can not trigger the next action when the user clicks a notification, but instead just fires an event. If your activity registers the BroadcastReceiver programmatically, it should be able to receive this event, and then update itself and run the next action as you wish.

+1
source

All Articles