2

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);
        }
    }
}
wattostudios
  • 8,666
  • 13
  • 43
  • 57
Stian
  • 23
  • 2

1 Answers1

1

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...

Community
  • 1
  • 1
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • if you have any problem in making appwidget click able then tell me i will help u.thanks – ρяσѕρєя K May 26 '12 at 14:59
  • Thanks! I took a look at that answer and your code, and after wrapping my head around it, I got to the point where I get a Toast when clicking my button, so I think I'll manage to add the code for sending texts too. I am adding my code inside the if statement in onRecieve, though I am not sure if this is the right place for it. – Stian May 26 '12 at 16:20