Skip to content

Instantly share code, notes, and snippets.

@yerenutku
Last active December 10, 2016 19:43
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 yerenutku/aefda7f82d4a895d0189cd397961d828 to your computer and use it in GitHub Desktop.
Save yerenutku/aefda7f82d4a895d0189cd397961d828 to your computer and use it in GitHub Desktop.
public class SimpleAppWidget extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
private void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.simple_app_widget);
// Construct an Intent object includes web adresss.
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://erenutku.com"));
// In widget we are not allowing to use intents as usually. We have to use PendingIntent instead of 'startActivity'
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
// Here the basic operations the remote view can do.
views.setOnClickPendingIntent(R.id.tvWidget, pendingIntent);
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment