Skip to content

Instantly share code, notes, and snippets.

@rassie
Created April 4, 2010 22:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rassie/355756 to your computer and use it in GitHub Desktop.
Save rassie/355756 to your computer and use it in GitHub Desktop.
public class MyWidget extends AppWidgetProvider {
private static final String TAG = "My Widget";
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (intent.getAction().equals("org.namespace.ACTION_1")) {
Toast.makeText(context, "Action 1", Toast.LENGTH_SHORT).show();
}
if (intent.getAction().equals("org.namespace.ACTION_2")) {
int param = intent.getIntExtra("org.namespace.param", 99);
Toast.makeText(context, "Param: " + param, Toast.LENGTH_SHORT).show();
}
}
@Override
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];
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main);
Intent[] intents = new Intent[5];
for (int a = 0; a < 5 ; a++) {
intents[a] = new Intent("org.namespace.ACTION_2").putExtra("org.namespace.param", a);
}
views.setOnClickPendingIntent(R.id.button_1, PendingIntent.getBroadcast(context, 0, new Intent("org.namespace.ACTION_1"), 0));
views.setOnClickPendingIntent(R.id.button_2, PendingIntent.getBroadcast(context, 0, intents[0], 0));
views.setOnClickPendingIntent(R.id.button_3, PendingIntent.getBroadcast(context, 0, intents[1], 0));
views.setOnClickPendingIntent(R.id.button_4, PendingIntent.getBroadcast(context, 0, intents[2], 0));
views.setOnClickPendingIntent(R.id.button_5, PendingIntent.getBroadcast(context, 0, intents[3], 0));
views.setOnClickPendingIntent(R.id.button_6, PendingIntent.getBroadcast(context, 0, intents[4], 0));
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment