Stian
Stian

Reputation: 23

Performing code when pressing a button in an Android AppWidget

I am working on a small app that lets me specify a phonenumber and a message, and then quickly send the preset message to the preset number. I've got the main functionality up and running, and now I want to make a widget.

I've been playing around with various widget tutorials, but none have covered what I want to do. They all end up in either just updating a clock, or launching an activity. What I wish to do is simply sending a message when the button in my widget is pressed, maybe a little Toast to say "Message sent".

How do I add an onClickListener to the button without having to have it launch an activity?

My widget class looks pretty much like the example on the official dev guide

public class SaldoWidget extends AppWidgetProvider {

    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, SaldoActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget1);
            views.setOnClickPendingIntent(R.id.button, pendingIntent);

            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }
}

Upvotes: 2

Views: 277

Answers (1)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132982

For Updating Your Home Screen widget on Button Click You need to Register a custom broadcast receiver in Manifest and add it as action with widget.

Make Your Widget Clickable is four-five step task,it's not possible describe all steps here.so see Android widget not updating my answer and download working home screen appwidget which generate random number on click from HomeWidgetRendomNumber.I Hope This Will Helpful...

Upvotes: 1

Related Questions