首页 > 解决方案 > 更新小部件

问题描述

我希望我的应用程序中的小部件自动更新,而无需用户进入应用程序。所以这是我的代码。

public class NewsAppWidgetProvider extends AppWidgetProvider {

static ArrayList<News> newsArrayList = new ArrayList<>();

static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                            int appWidgetId) {

    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_grid_view);

    // Construct the RemoteViews object


    // Set the GridWidgetService intent to act as the adapter for the GridView
    Intent intent = new Intent(context, GridWidgetService.class);
    views.setRemoteAdapter(R.id.widget_grid_view, intent);

    Intent templateIntent = new Intent(context, DetailActivity.class);
    templateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    PendingIntent templatePendingIntent = PendingIntent.getActivity(
            context, 0, templateIntent, 0);

    views.setPendingIntentTemplate(R.id.widget_grid_view,
            templatePendingIntent);
    // Instruct the widget manager to update the widget
    appWidgetManager.updateAppWidget(appWidgetId, views);
}

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

public static void updateRecipeWidgets(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    for (int appWidgetId : appWidgetIds) {
        updateAppWidget(context, appWidgetManager, appWidgetId);
    }
}

@Override
public void onEnabled(Context context) {
    super.onEnabled(context);
}

@Override
public void onDisabled(Context context) {
    super.onDisabled(context);
}

@Override
public void onReceive(Context context, Intent intent) {
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, NewsAppWidgetProvider.class));

    final String action = intent.getAction();

    if (action.equals("android.appwidget.action.APPWIDGET_UPDATE2")) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        Gson gson = new Gson();

        String json = prefs.getString("news", "");
        Type type = new TypeToken<ArrayList<News>>(){}.getType();
        newsArrayList = gson.fromJson(json, type);
        appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.widget_grid_view);
        //Now update all widgets
        NewsAppWidgetProvider.updateRecipeWidgets(context,appWidgetManager,appWidgetIds);
        super.onReceive(context,intent);
    }
}
}

那么我该怎么做呢?

更新

我做了一些更改,首先我在调用onUpdate的updateAppWidget中读取了带有 Gson 的 json 文件。接下来,我在onReceive里面使用标准动作android.appwidget.action.APPWIDGET_UPDATE所以。

public class NewsAppWidgetProvider extends AppWidgetProvider {

static ArrayList<News> newsArrayList = new ArrayList<>();
private static String json;
private static Type type;
private static SharedPreferences prefs;
private static Gson gson;

static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                            int appWidgetId) {

    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_grid_view);

    int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, NewsAppWidgetProvider.class));

    prefs = PreferenceManager.getDefaultSharedPreferences(context);
    gson = new Gson();
    json = prefs.getString("news", "");
    type = new TypeToken<ArrayList<News>>(){}.getType();
    newsArrayList = gson.fromJson(json, type);
    appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.widget_grid_view);
    // Construct the RemoteViews object
    // Set the GridWidgetService intent to act as the adapter for the GridView
    Intent intent = new Intent(context, GridWidgetService.class);
    views.setRemoteAdapter(R.id.widget_grid_view, intent);

    Intent templateIntent = new Intent(context, DetailActivity.class);
    templateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    PendingIntent templatePendingIntent = PendingIntent.getActivity(
            context, 0, templateIntent, 0);

    views.setPendingIntentTemplate(R.id.widget_grid_view,
            templatePendingIntent);
    // Instruct the widget manager to update the widget
    appWidgetManager.updateAppWidget(appWidgetId, views);
}

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

public static void updateRecipeWidgets(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    for (int appWidgetId : appWidgetIds) {
        updateAppWidget(context, appWidgetManager, appWidgetId);
    }
}

@Override
public void onEnabled(Context context) {
    super.onEnabled(context);
}

@Override
public void onDisabled(Context context) {
    super.onDisabled(context);
}

@Override
public void onReceive(Context context, Intent intent) {
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, NewsAppWidgetProvider.class));

    final String action = intent.getAction();

    if (action.equals("android.appwidget.action.APPWIDGET_UPDATE")) {
        NewsAppWidgetProvider.updateRecipeWidgets(context,appWidgetManager,appWidgetIds);
        super.onReceive(context,intent);
    }
  }
}

标签: androidwidget

解决方案


推荐阅读