i can update a widget but i have some troubles starting a new intent on click from the widget.

This is the code:

        try { 
        remoteViews.setOnClickPendingIntent(R.id.widgetIcon,
                    openGooglePlay(context, bundle));

        } catch (NullPointerException e) {
            Log.d(TAG,
                    "Error loading google play from widget: " + e.getMessage());
        }

I am confused why this intent is not started on click. It should be attached to resource widgetIcon only and so not be confused with anything else, right?

有帮助吗?

解决方案

Make sure the AppWidgetprovider onUpdate method is called, and your setting the right pending intent. Try to debug and see if onUpdate in called when you add the widget in home screen.

Here is a sample working code..

public class CustomAppWidgetProvider extends AppWidgetProvider{
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    final int N = appWidgetIds.length;

    for (int i = 0; i < N; i++) {
        int appWidgetId = appWidgetIds[i];

        Intent intent = new Intent(context, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.activity_main);
        views.setOnClickPendingIntent(R.id.button1, pendingIntent);

        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
 }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top