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);
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?