Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
310 views
in Technique[技术] by (71.8m points)

onclick - Start intent from Android Widget on click

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

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

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...