android - 更新小部件
问题描述
我希望我的应用程序中的小部件自动更新,而无需用户进入应用程序。所以这是我的代码。
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);
}
}
}
解决方案
推荐阅读
- c++ - 我在哪里得到“GDI_CapturingAnImage.h”?我想要 GDI_CapturingAnImage.h 文件
- flutter - 如何在颤动中增加滑块轨道宽度
- java - JPA 批量插入不会提高性能
- r - 是否有用于将表格按两行分组的 R 函数?
- python - 当我们拆分训练和测试时,我们希望将目标变量设为动态
- ionic-framework - 当互联网未连接到设备时本地存储数据
- opencv - OpenCV:如何关闭二进制图像中的边缘
- javascript - 如何无限循环一个函数(而不是使用传统的for循环)
- hadoop - 直线中 Tez 作业的作业名称并在 YARN 中查看
- elasticsearch - 在 ElasticSearch 中为审计编制索引