Skip to content

Instantly share code, notes, and snippets.

@yerenutku
Created December 11, 2016 16:44
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/f71f1522f01b5ced9f4cdec9a84c4496 to your computer and use it in GitHub Desktop.
Save yerenutku/f71f1522f01b5ced9f4cdec9a84c4496 to your computer and use it in GitHub Desktop.
public class UpdateService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// generates random number
Random random = new Random();
int randomInt = random.nextInt(100);
String lastUpdate = "R: "+randomInt;
// Reaches the view on widget and displays the number
RemoteViews view = new RemoteViews(getPackageName(), R.layout.updating_widget);
view.setTextViewText(R.id.tvWidget, lastUpdate);
ComponentName theWidget = new ComponentName(this, UpdatingWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(theWidget, view);
return super.onStartCommand(intent, flags, startId);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment