Alex
Alex

Reputation: 31

Android PendingIntent to start Activity

I have managed to setup my widget so that when it is clicked it starts a config activity so a user can change previously set data. However when the widget is first added to the homescreen clicking on it does nothing, If I push the apk again through eclipse without editing any code the widget then becomes clickable, I have tried putting the same PendingIntent in the onEnable method but it still doesn't work?

Here is my on update method

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
                    int[] appWidgetIds) {
    for (int appWidgetId : appWidgetIds) {
            setAlarm(context, appWidgetId, UPDATE_RATE);
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main);

            //views.setViewVisibility(R.id.button1, View.VISIBLE);

            Intent intent = new Intent(context, Config.class);
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); 
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
            PendingIntent pendIntent = PendingIntent.getActivity(context, appWidgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            views.setOnClickPendingIntent(R.id.imageView2, pendIntent);

            appWidgetManager.updateAppWidget(appWidgetId, views);


        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }

}

If im correct the onRecieve method is not needed anymore as I am not sending a broadcast im asking for an activity?

Thanks in ADvance

Upvotes: 1

Views: 6307

Answers (1)

Alex
Alex

Reputation: 31

For anyone who wants to know I had to add the following code to my service onStart method once I add the widget the onClick was instantly available!

 RemoteViews views = new RemoteViews(getApplicationContext().getPackageName(), R.layout.main);
            Intent intent1 = new Intent(getApplicationContext(), Config.class);
            intent1.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); 
            intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            intent1.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
            PendingIntent pendIntent = PendingIntent.getActivity(getApplicationContext(), appWidgetId, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
            views.setOnClickPendingIntent(R.id.imageView2, pendIntent);

Upvotes: 2

Related Questions