Skip to content

Instantly share code, notes, and snippets.

@azzumw
Created January 14, 2019 21:46
Show Gist options
  • Save azzumw/51834fc454c73ecdb505cd7f6269fcc5 to your computer and use it in GitHub Desktop.
Save azzumw/51834fc454c73ecdb505cd7f6269fcc5 to your computer and use it in GitHub Desktop.
package com.example.macintosh.thebakingappproject;
import android.app.IntentService;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.Nullable;
public class WidgetUpdateService extends IntentService {
public static final String ACTION_UPDATE_LIST_VIEW = "com.example.macintosh.thebakingappproject.widgetupdateservice.update_app_widget_list";
public WidgetUpdateService() {
super("WidgetUpdateService");
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
if (intent != null) {
final String action = intent.getAction();
if(ACTION_UPDATE_LIST_VIEW.equals(action)){
handleActionUpdateListView();
}
}
}
private void handleActionUpdateListView(){
//get the recipe data
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(this, WidgetProvider.class));
WidgetProvider.updateAllAppWidget(this, appWidgetManager,appWidgetIds);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.widgetListView);
}
public static void startActionUpdateAppWidgets(Context context) {
Intent intent = new Intent(context, WidgetUpdateService.class);
intent.setAction(ACTION_UPDATE_LIST_VIEW);
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// context.startForegroundService(intent);
// }
// else {
// context.startService(intent);
// }
context.startService(intent);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment