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);
}
}
}