Skip to content Skip to sidebar Skip to footer

How Do I Run An Activity From A Button On A Widget In Android?

I'm working on a toggle widget that consists of a button. When pressed, I'd like it to run an activity without opening up anything (just saying on the desktop as usual). Is there a

Solution 1:

start activity on Home Screen Widget click:

Method 1: in on onReceive

Intent intentClick =   the new Intent to (context, update, class);
PendingIntent pendingIntent = PendingIntent.getActivity (context, 0,
intentClick, 0);
rv.setOnClickPendingIntent (R.id.layout, pendingIntent);

Method 2: in onReceive

Intentintn=newIntent (context, update. Class);
intn.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity (intn);

but you must register a broadcast for widget click

Solution 2:

If you want to do sth. in the background, you can use Service instead of Activity: http://developer.android.com/reference/android/app/Service.html

1.you can starService as soon as you app started, or your widget displayed. http://developer.android.com/reference/android/content/Context.html#startService(android.content.Intent)

2.try to bindService when the widge is clicked http://developer.android.com/reference/android/content/Context.html#bindService(android.content.Intent, android.content.ServiceConnection, int)

3.after binded, you can get the service instance from the binder using serviceConnection. Then you can executed the code defined in the service. see the example here :http://developer.android.com/reference/android/app/Service.html

or you can just

  1. try to start service(Only on service instance will be running in every moment)

  2. send broadcast to the service which ask the service to do sth. for you.

Since the activity is designed to display sth to user, and the service is designed to execute in the background.

Solution 3:

An alterative is to send a broadcast intent rather starting an activity! I wanted to perform an action when a widget button was pushed -- not specially to start an activity that would show up on screen. You can use this technique to execute your code in the widget itself (where the widget does not necessarily contain an Activity.)

Intentintt=newIntent(ctx, RWidg.class);
intt.setAction("myActionName");
PendingIntentbcInt= PendingIntent.getBroadcast(ctx, 0, intt, 0);

RemoteViewsviews=newRemoteViews(ctx.getPackageName(), R.layout.widg_lay);
views.setOnClickPendingIntent(R.id.button, bcInt);

appWidgetManager.updateAppWidget(widgId, views);

Rather than using a PendingIntent to start an activity, using the PendingIntent.getBroadcast() creates a PendingIntent with 'secret key': ActivityManager.INTENT_SENDER_BROADCAST which causes the ActivityManager to do a sendBroadcast() rather than a startActivity()

You need to add an 'if' statement to your onRecieve() method to intercept the action name or names that you use:

publicvoidonReceive(Context ctx, Intent intt) {
  String action = intt.getAction();
  if (action.compareTo("myActionName")) {
    // Do your myActionName work here
  } else {
    super.onReceive(ctx, intt);
  }
} // end of onReceive

Post a Comment for "How Do I Run An Activity From A Button On A Widget In Android?"