I'm trying to get my home screen widget to trigger the config class for a new widget but am having a problem.
When a user tries to add my widget to the home screen for the first time it kicks off a thread which downloads a file with a list of sensors and then exits. Once the download is complete the download task triggers a notification. When the user taps on the notification it should start the dataWidgetConfig activity. This works, to a point using this code
mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
final Notification notifyDetails = new Notification(R.drawable.icon,"Sensor list downloaded.",System.currentTimeMillis());
CharSequence contentTitle = "Notification Details...";
CharSequence contentText = "Sensor list has downloaded, tap to add widget.";
Intent notifyIntent = new Intent(self, dataWidgetConfig.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
notifyIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
PendingIntent pendingIntent = PendingIntent.getActivity(self, appWidgetId, notifyIntent, 0);
notifyDetails.setLatestEventInfo(self, contentTitle, contentText, pendingIntent);
mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);
My problem is, that I don't have an appWidgetId. When I add a widget via long press on the home screen I get allocated an appWidgetId, how do I ask the home screen for one? I'm guessing I can't just make one up since it might either be in use already or get allocated later.
At the moment my activity launches ok, but exits because I've deliberately set my appWidgetId as:
AppWidgetManager.INVALID_APPWIDGET_ID;
Can anyone give me any ideas on this?
Related
I am able to generate and display multiple notifications, all starting the same activity but displaying different information depending on the extra that is attached to the intent that starts up the Activity. When I am within my application, all the notifications are able to start the Activity, but when I am outside of the application, only one is able to start the Activity (the first that I click on), the rest do nothing once clicked on (except disappear). This is my code for generating the notifications. The "reqCode" represents unique numbers.
Resources r = getResources();
Intent i = new Intent(this, DisplaySearchTickerAlert_Activity.class);
i.putExtra(NewArticleNotification_Service.QUERY_TICKER, searchQuery);
PendingIntent pi = PendingIntent.getActivity(this, reqCode, i, 0);
Notification notification = new NotificationCompat.Builder(this)
.setTicker(r.getString(R.string.ticker_alert))
.setSmallIcon(android.R.drawable.ic_menu_call)
.setContentTitle(r.getString(R.string.ticker_alert))
.setContentText("Newly published for search term: " + "\""
+ searchQuery + "\"").setContentIntent(pi).setAutoCancel(true).build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(reqCode, notification);
That is because your activity is already being displayed. Only the first notification will bring it to the foreground. The rest of notifications will dispatch a new intent that depending on your activity android:launchMode you might be able to catch on the onNewIntent callback.
I have a notification in my app and I want it to bring back an existing activity when the user clicks it.
The notification is genereated within the activity I want to bring back so I assume it still exists.
This is my code for the notification:
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification noti = new Notification(R.drawable.icon, "s", 0);
CharSequence title = "S";
CharSequence details = "W";
Intent intent = new Intent(getBaseContext(), Start.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
PendingIntent pending = PendingIntent.getActivity(getBaseContext(), 0, intent, 0);
noti.setLatestEventInfo(Start.this, title, details, pending);
nm.notify(0,noti);
But for some reason it keeps creating a new activity.
what am I doing wrong?
"The notification is genereated within the activity I want to bring back so I assume it still exists." Well, it may, but it's not generally a good idea for you to count on that. What if the user clicks on the notification only after backing out of your app using the back button, or after some length of time during which the OS has killed your activity while the user has been doing other things? Is there a particular reason why you need the "old" activity? If it has a state that you need to preserve or recreate, you can do that: have a look at http://developer.android.com/training/basics/activity-lifecycle/recreating.html and http://developer.android.com/guide/components/activities.html#SavingActivityState.
i have an appwidget that runs successfully. now when the phone is being rebooted,
the widget loses all its data and just sits on the home screen. since i can't update the widget from a broadcast receiver when the phone reboots, i created a notification
that leads to the configuration activity of the widget. Nothing works after the user sets the configuration again and leaves the configuration activity; the idle widget just remains there?(the user has to delete the widget and create a widget again).. Am assuming i am not receiving the widget id correctly or am i doing it wrongly in broadcast receiver and suppose to move all the code to onEnable in the widget method?.. How do i refresh the widget correctly. Please bear in mind that all widget updates are done from a service.
by the way i have this code in broadcast receiver for boot_completed action:
public void onReceive(final Context context, Intent intent) {
String info = context.getResources().getString(R.string.restart_setting);
int[] allids = AppWidgetManager.getInstance(context).getAppWidgetIds(new ComponentName(context, MyWidgetProvider.class));
for(int appWidgetId:allids){
NotificationManager mnotifyManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notify = new Notification(R.drawable.icon, "Weather Update", System.currentTimeMillis());
notify.defaults = Notification.DEFAULT_SOUND;
notify.defaults = Notification.DEFAULT_VIBRATE;
Intent Settings = new Intent(context, WidgetConfigure.class);
Settings.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
PendingIntent pending = PendingIntent.getActivity(context, 0, weatherSettings, PendingIntent.FLAG_ONE_SHOT);
notify.setLatestEventInfo(context, "Weather Update", info, pending);
notify.flags = Notification.FLAG_AUTO_CANCEL;
notify.defaults = Notification.DEFAULT_ALL;
mnotifyManager.notify(WEATHER_NOTIFICATION_ID , notify);
}
}
I just had the same problem this morning and tried to solve it with listening to the boot_completion intent. It doesn't work this way with widgets. Here's what I found.
To get notified about a reboot, you need to receive android.intent.action.ACTION_EXTERNAL_APPLICATION_AVAILABLE in addition to boot_completed and the permission of android.permission.RECEIVE_BOOT_COMPLETED.
But the whole truth is that an additional intent is not necessary. After reboot the onEnable and onUpadate of the BroadcastReceiver get called anyway.
So, the solution I've implemented, stores the config of each widget in a file, with the widget Id as part of the filename. And in onUpdate of the receiver I initialize the widget again (with click listener and all that)
Outcome is, that after reboot it takes a moment but then the widgets (all) look good and work like expected.
I'm a newbie in Android developing and after reading the documentation about tasks and activities I can't get my application working correctly.
(First of all, sorry for my English)
My application consist of two activities: LOGIN and NEWS. Both activities launching method is singleTask.
The NEWS activity creates a notification with onCreate with the standard notification code of the Android notification tutorial!.
int icon = R.drawable.notification_icon; // icon from resources
CharSequence tickerText = "Hello"; // ticker-text
long when = System.currentTimeMillis(); // notification time
Context context = getApplicationContext(); // application Context
CharSequence contentTitle = "My notification"; // expanded message title
CharSequence contentText = "Hello World!"; // expanded message text
Intent notificationIntent = new Intent(this, MyClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
When I first open the application:
LOGIN --> onResume() --> NEWS --> onCreate() --> Notification
With the code
Intent newLogAct = new Intent(Login.this, News.class);
TomTuckerActivity.this.startActivity(newLogAct);
If I hit Back *NEWS* is destroyed and again:
LOGIN --> onResume() --> NEWS --> onCreate() --> Notification
(I don't like that loop, the reason of using it is explained at the end)
If I hit Home I go back to the main menu and here begin what I don't understand:
If I use the notification to relaunch the application there is no problem and NEWS window is opened again without calling onCreate and without sending the notification.
If I use the application icon when calling NEWS the singleTask option seems to be useless because onCreate() is called again and the notification is sent again.
What I want is to recover the application where I left it either I use the notification or the icon.
May a flag in the newLogAct will solve the problem?
Is OK tho have singleTask launching option in both activities?
About the Back button loop problem:
To avoid the loop I thought about using onCreate() instead of onResume(). However, when I relaunches the application with the application icon LOGIN is loaded but onCreate is not called so NEWS is not loaded.
Is there any other way to solve that?
Maybe with onNewIntent() method?
Your problem (or at least a part of it) seems to be: How to make a notification come back where you left the application.
Notification are not supposed to be used this way, clicking on a notification should start a new activity whose aim is to deal with the notification.
If you do want to come back to the application where you left it (I’m doing it), you can use the following trick (I don’t know if this is good practice, but it works and does not seems that hackish) : create an Activity called Autodestruct which call finish() in its onCreate() method, and make the notification run this Activity. This will restore the back stack with a dummy Activity on top of it and remove the dummy Activity immediately.
1/I don't want to notify user if this one is already running my app in foreground,
is it possible before create nofification to check if my app is not in front ?
2/if app is in background, is it possible to bring last state in front ?
I know that Os can destroy some Activity, but is it possible to restore last state, don't start a new Intent?
because if i start a new Intent and i push back, old Intent appear it's not very beautiful to have 2 identical intent launch.
Thanks
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
Intent notificationIntent = new Intent(this, z_finish.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new Notification(icon, tickerText, when);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);
is it possible before create nofification to check if my app is not in front ?
Your activities will have to tell the service when they are being paused and resumed; the service can then decide whether to display the Notification or not.
if app is in background, is it
possible to bring last state in front
?
I am not quite certain what "last state" is. Try FLAG_ACTIVITY_CLEAR_TOP in your Intent.
if your app is in singletask mode then the last state will be saved even if you relaunch it