Launching Activity From Widget
Solution 1:
Bringing this way back from the dead, but I had a similar problem and I think I finally solved it... like you, I had a PendingIntent that I attached to the RemoteView. Sometimes it would work, and sometimes it would fail. It was driving me crazy.
What I found from a tooltip on the PendingIntent.getActivty() was:
Note that the activity will be started outside of the context of an existing activity, so you must use the Intent.FLAG_ACTIVITY_NEW_TASK launch flag in the Intent.
so, I added:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
No example code I've seen so far does this, but it solved my problem; the Settings activity is now launched reliably.
The full code that's working well...
Intentintent=newIntent(context, Settings.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appId); // Identifies the particular widget...
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Make the pending intent unique...
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntentpendIntent= PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViewsviews=newRemoteViews(context.getPackageName(), R.layout.wwwidget);
views.setOnClickPendingIntent(R.id.widget, pendIntent);
appWidgetManager.updateAppWidget(appId,views);
Solution 2:
I was having the same issue. I discovered that the fix is to call an update through the appwidget manager. here is an example of how to do that in onEnabled. It appears it needs to be done in both onEnabled and onUpdated so that when device is powering on your click intent is also intialized - in onUpdated the params already provide the reference to the manager, luckily.
@OverridepublicvoidonEnabled(Context context) {
//Log.v("toggle_widget","Enabled is being called"); AppWidgetManagermgr= AppWidgetManager.getInstance(context);
//retrieve a ref to the manager so we can pass a view update Intenti=newIntent();
i.setClassName("yourdoman.yourpackage", "yourdomain.yourpackage.yourclass");
PendingIntentmyPI= PendingIntent.getService(context, 0, i, 0);
//intent to start service // Get the layout for the App Widget RemoteViewsviews=newRemoteViews(context.getPackageName(), R.layout.togglelayout);
//attach the click listener for the service start command intent
views.setOnClickPendingIntent(R.id.toggleButton, myPI);
//define the componenet for self ComponentNamecomp=newComponentName(context.getPackageName(), ToggleWidget.class.getName());
//tell the manager to update all instances of the toggle widget with the click listener
mgr.updateAppWidget(comp, views);
}
Solution 3:
This worked for me, based on info here, the word widget sample, and the tutorial here
Intentintent=newIntent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
// first param is app package name, second is package.class of the main activityComponentNamecn=newComponentName("com....","com...MainActivity");
intent.setComponent(cn);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntentmyPI= PendingIntent.getActivity(context, 0, intent, 0);
RemoteViewsviews=newRemoteViews(context.getPackageName(), R.layout.widget_word);
views.setOnClickPendingIntent(R.id.widget, myPI);
AppWidgetManagermgr= AppWidgetManager.getInstance(context);
mgr.updateAppWidget(comp, views);
Solution 4:
The problem with the Toast not showing is easy, you don't call show(), a mistake I always do too... do
Toast.makeText(context, "Hello from onUpdate", Toast.LENGTH_SHORT).show();
instead of
Toast.makeText(context, "Hello from onUpdate", Toast.LENGTH_SHORT);
Solution 5:
When adding the widget to the homescreen, Logcat shows the two debugging lines, though not the Toast. (Any ideas why not?)
Don't try launching Toasts
from a BroadcastReceiver
.
Is there a way to test where the fault is?
Look at LogCat, via adb logcat
, DDMS, or the DDMS perspective in Eclipse. You may find warnings about not finding an activity to match the given Intent
.
I do not see any obvious problem. You may want to take a peek at one of my book examples and see if that works for you, and if it gives you any idea of what may be afoot.
Post a Comment for "Launching Activity From Widget"