0

Is there any work around to call an activity directly from a button on a widget? Like a beautiful button to launch the app.

From the doc and some answers here, views.setOnClickPendingIntent is the only way and that requires a service. But I don't need a service cause I'm not really updating the widget!

Actually, my original task is quite simple. I want an icon on home screen that calls an activity, but I don't want that icon appears in app-drawer. I know I can put a lot of activity icons in app-drawer.

wez
  • 337
  • 3
  • 16

1 Answers1

0

You dont need a service for that.

You should just setOnClickPendingIntent in the onUpdate method in your AppWidgetProvider.

here are some links that show how it can be done, they are basically the same with some variations, you should try them out:

Start activity by clicking on widget

Launching activity from widget

How do I run an Activity from a button on a widget in Android?

How Do I Launch an Activity with Home Widget

Like i said there is no need for a service.

here is the sample code i used:

    @Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){

     RemoteViews remoteViews=new RemoteViews(context.getPackageName(), R.layout.testwidget);
     //Intent set with the activity you want to start
     Intent intent = new Intent(context, MainActivity.class);


     PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

    remoteViews.setOnClickPendingIntent(R.id.textView1, pendingIntent);

    appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
    }
Community
  • 1
  • 1
JanBo
  • 2,925
  • 3
  • 23
  • 32
  • Thanks, JanBo! I see most examples has "super.onUpdate(context, appWidgetManager, appWidgetIds);" in start of the onUpdate, but not yours, is it good practice? I see no impact without it. – wez Aug 14 '13 at 09:42
  • Hm, it is a good practice to include it, here is some explanation on it http://stackoverflow.com/questions/10860793/super-onupdate-in-appwidgetprovider-recommend . Like you said it works without it but its bad practice in general. – JanBo Aug 14 '13 at 09:51