我希望我的应用中的小部件能够自动更新,而无需用户输入应用。所以这是我的代码。
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);
}
}
}
那我怎么能这样做?
更新
我首先在 updateAppWidget 中读取了带有Gson的json文件进行了一些更改,该文件在 onUpdate 中调用。接下来,我在 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);
}
}
}