Is it possible to use startActivityForResult() from a status bar notification?
Say I have an activity A, which on some event starts activity B using startActivityForResult(). Now when it is in the background, on the event it shows a notification. Now on selecting the notification, how do i start activity B for result?
I do realize that activity A should have a service that runs in the background, but i guess the same question would apply even in that case.
Here's the code for the notification. This is in the Activity A.
Notification notification = new Notification(R.drawable.ic_launcher, "New Notification", System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL;
CharSequence contentTitle = "My Notification Title";
CharSequence contentText = "My Notification Text";
Intent notificationIntent = new Intent(this, ActivityB.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(getApplicationContext(), contentTitle, contentText, contentIntent);
nm.notify(1, notification); //1 = id
I think that you should just start the activity A from the activity B when the activity B is opened by opening the notification and then closed.
You can pass the return value in the intent that you use to start the activity A from the activity B.
Dont use startActivityForResult. You can achieve the same functionality in different way.
Pass the result as an extra with your notification. Get this result in onResume of activity B.
Related
I stuck in a problem.
Suppose there is an activity A
now, user press Home key, and put A into background.
now, after remaining in background for a while, it will generate a notification.
On clicking on that notification, it will bring back the exact same activity which was in the background.
Like, after pressing home key, if user start the application again by clicking on the icon or from the recent launched history, then android re-opens exactly last activity in it's previous state.
Here, I dont want to detect which was the last activity. My activity is fixed, I just want to bring it back at its previous state on clicking on the notification ?
Is that possible ? how to do that ?
here is my code for notification,
private void generateNotification(Context context, String message, String data) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.notification, message, System.currentTimeMillis());
Intent notificationIntent = new Intent(context, MenuActivity.class);
//notificationIntent.setFlags(Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
Bundle bundle = new Bundle();
bundle.putString("data",data);
notificationIntent.putExtras(bundle);
//Constants.NOTIFICATION_ID++;
notification.setLatestEventInfo(context, context.getString(R.string.app_name), message, PendingIntent.getActivity(context, 0, notificationIntent, Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR));
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(Constants.NOTIFICATION_ID, notification);
}
Use Intent.FLAG_ACTIVITY_CLEAR_TOP instead of FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY.
I raised a notification . What i want is , that when the user clicks on the notification , my activity is brought to the front without creating a new instance of it .
For this , I added the flag , REORDER_TO_FRONT , but still oncreate is being called instead of onNewIntent when i click on the notification .
This is my code -
int icon = R.drawable.android;
long when = System.currentTimeMillis();
CharSequence text = "new message";
CharSequence contentTitle = stanza.getFrom(); // message title
CharSequence contentText = stanza.getBody(); // message text
Intent notificationIntent = new Intent(context, ChatBox.class);
notificationIntent.putExtra("buddyid",stanza.getFrom());
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,notificationIntent, 0);
// the next two lines initialize the Notification, using the configurations above
Notification notification = new Notification(icon, text, when);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(1,notification);
Have u tried:
Intent.FLAG_ACTIVITY_CLEAR_TOP
with your notificationIntent.addFlag();
The solution for me for this was to make a broadcast receiver that listens to broadcast actions that the notification triggers. So basically:
Notification triggers a broadcast action with an extra the name of the activity to launch.
Broadcast receiver catches this when the notification is clicked, then creates an intent to launch that activity using the FLAG_ACTIVITY_REORDER_TO_FRONT flag
Activity is brought to the top of activity stack, no duplicates.
I have 3 Activities
A. MainActivity - just an Activity with a text and button on it
B. SettingsActivity - this will be displayed if the button on (A) is clicked, this initializes time picker that will send a notification when it alarms
C. DisplayNotification - activity for displaying notification in the status bar, when clicked, it should show A
This is what I want to happen:
I launch my application, A will be shown
I click the button, B will be shown
I set a time for the alarm
When the alarm triggers, a notification is shown in the status bar
If I click the notification, A will be shown
This is what happens (I have 2 scenarios with different results):
First scenario:
After step 3, I tap the back button, A will now be shown
Then, I tap again the back button, the onDestroy of A will be called and screen will show the 'home page' or 'desktop' of the phone
I wait till the alarm triggers
When the alarm triggers, notification is shown on the status bar
I click the notification, it launches my app displaying A
End of story. This is what is expected to happen
Second scenario: (the buggy one)
After step 3, I tap the HOME button, A's onDestroy is not yet called and the latest screen was B
The screen shows the 'home page' or 'desktop' of the phone
I wait till the alarm triggers
When the alarm triggers, notification is shown on the status bar AND B was shown automatically without me clicking the notification on the status bar
What I want to happen here is that it should not automatically display B activity.
Am I missing any flags for notification or intent?
Here is the code snippet for Activity B which triggers the alarm
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent i = new Intent(this, DisplayNotification.class);
i.putExtra("NotifID", 1);
PendingIntent displayIntent = PendingIntent.getActivity(getBaseContext(), 0, i, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, displayIntent);
Here is the code for Activity C
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int notifID = getIntent().getExtras().getInt("NotifID");
Intent i = new Intent(this, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
i.putExtra("NotifID", notifID);
PendingIntent detailsIntent = PendingIntent.getActivity(this, 0, i, 0);
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notif = new Notification(R.drawable.ic_launcher, "See activity!", System.currentTimeMillis());
notif.flags |= Notification.FLAG_AUTO_CANCEL;
notif.defaults |= Notification.DEFAULT_SOUND;
CharSequence from = "Notification from me";
CharSequence message = "See activity!";
notif.setLatestEventInfo(this, from, message, detailsIntent);
nm.notify(notifID, notif);
finish();
}
I hope someone would help me :)
Thanks!
Edit:
Thanks Chirag_CID! I should have used Broadcast Receiver instead of another Activity. So, instead of extending Activity, the DisplayNotification now extends Broadcast Receiver. This is the onReceive method of DisplayNotification:
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "DisplayNotification onReceive");
int notifID = intent.getExtras().getInt("NotifID");
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
i.putExtra("NotifID", notifID);
PendingIntent detailsIntent = PendingIntent.getActivity(context, 0, i, 0);
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notif = new Notification(R.drawable.ic_launcher, "See the quote of the day!", System.currentTimeMillis());
notif.flags |= Notification.FLAG_AUTO_CANCEL;
notif.defaults |= Notification.DEFAULT_SOUND;
CharSequence from = "Notification";
CharSequence message = "See the activity!";
notif.setLatestEventInfo(context, from, message, detailsIntent);
nm.notify(notifID, notif);
}
I read the whole Question 2-3 times..
Now I want to point to few things up there,
You have made Activity C in which you are creating notification which will call MainActivity on it's Click.
But, When you set alarm from B you are passing intent of Activity C, Though your code of C doesn't have setContentView method, and Also you have written finish() in the end of onCreate() so when Activity C is called through the Alarm set from B, It just call Activity C and set Notification ..but As you written finish() it ends Activity C and shows Activity B latest from the Stack.
If You want to cross check..
Write logs in onCreate(),onPause() and onDestroy() of Activity C..and you will see the Activity C is created and Destroyed when Alarm triggered.
Solution:
If you don't want to show any activity when you are on Home Screen and Alarm trigger then,
In Activity B where you written pending intent for Activity C..You will need it to change to call the Broadcast Receiver and use PendingIntent.getBroadcast where its PendingIntent.getActivity and the code you have written in C for notification you will have to write that in Receiver...hence none of your activity will be called when the Alarm Trigger.
Happy Coding :)
Hi i am new to android.
I am implementing a code with notification functionality. Here i have two activities in my application those are ActivityA, and ActivityB.
I want to start ActivityB from notification and i need to send some flag or some value to the ActivityB. How can i send the data like int value or flag to that called activity using notification on click. The problem is when i am launching activity from launcher icon first it will called ActivityA and from that ActivityA i am passing some value to ActivityB.
But when i am launching ActivityB from notification i con't send any values to that activity so it is giving force close.
To call activity from notification i am using this code
mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
final Notification notifyDetails = new Notification(R.drawable.android,"New Alert, Click Me!",System.currentTimeMillis());
Context context = getApplicationContext();
CharSequence contentTitle = "Notification Details...";
CharSequence contentText = "Browse Android Official Site by clicking me";
Intent notifyIntent = new Intent(Intent.ACTION_MAIN);
notifyIntent.setComponent(new ComponentName("mypackage","mypackage.ActivityB"));
PendingIntent intent = PendingIntent.getActivity(SimpleNotification.this, 0,notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
notifyDetails.setLatestEventInfo(context, contentTitle, contentText, intent);
mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);
Please advise me how can i send values from notification to called activity.
you must set your ActivityB in notifyIntent
Intent notifyIntent = new Intent(this, ActivityB.class); // 'this' - Context object
For sending values use extras
for example:
intent.putExtra("yourTag", yourVariable);
I want make a notification, that when clicked on it will bring my app from the background to the front. I am using the following code:
NotificationManager noma = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
PendingIntent pen = PendingIntent.getActivity(Timer.this, 0, intent, 0);
intent.putExtra("key", "trigerred");
String body = "This is a message";
String title = "This is title";
Notification no = new Notification(R.drawable.ic_launcher, body, System.currentTimeMillis());
no.defaults = Notification.DEFAULT_ALL;
no.setLatestEventInfo(this, title, body, pen);
noma.notify(uniqueID, no);
When I click on the notification that makes a new intent but I want the last created intent brought to the front. How i can do this?
You need to set the FLAG_ACTIVITY_SINGLE_TOP flag on the intent that you pass to getActivity.
This should bring you back to the all ready running activity when clicking your notification.
See here for a list of the different launch flags.
Try this
PendingIntent pen = PendingIntent.getActivity(Timer.this, 0, intent,Intent.FLAG_ACTIVITY_TASK_ON_HOME);