简体   繁体   中英

Trouble with launching an activity from a widget, when the widget has a configuration activity

I am having trouble with launching an activity by tapping on a home screen widget when the widget has a configuration activity.

Normally , I have the Intent and the PendingIntent in the onUpdate method in the widget provider class.But , from what understand , that doesn't get called when you have a configure activity for your widget.

basically , I need to know where to put this bit of code

Intent intent = new Intent(context, ClassToLaunch.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
views.setOnClickPendingIntent(R.id.widget, pendingIntent);  

Do let me know if you need to see the code for my configure activity and widget provider Thanks for taking the time to read this and for any help that you can give.

Cheers

As you have already see here , onUpdate is not called when you have a configure activity, the first time the widget is putted.

This is how it works : The user choose the widget is the list, and when he placed it on the spot he wants, the activity is bring on front automatically. The user can then configure what he needs. Hence, onUpdate is not called at this time ! But if later the user click on the widget, onUpdate will be called, and then you can handle the click to bring any activity on. I'm not sure you can call the configure acitivty to modifiy the widget like that, you will probably have to add code in order to make the changes work.

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        Intent intent = new Intent(context, ClassToLaunch.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
        views.setOnClickPendingIntent(R.id.widget, pendingIntent);  

        // Push update for this widget to the home screen
        ComponentName thisWidget = new ComponentName(context, WidgetProvider.class);
        AppWidgetManager manager = AppWidgetManager.getInstance(context);
        manager.updateAppWidget(thisWidget, views);


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM