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

Categories

0 votes
296 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...