Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
302 views
Welcome To Ask or Share your Answers For Others

1 Answer

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);
    }
 }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...