我正在尝试使用Home / Lock屏幕上的一些小部件实现一个Android应用程序! 目前,小部件内容在创建时是可以的,但没有保持最新。
这是我的代码 的 MyWidgetProvider
public class MyWidgetProvider extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
Integer[] preferencesIds = MyPreferences.getWidgetIds(context);
Log.d(MyWidgetProvider.class.getSimpleName(), "In SP there is " + Arrays.toString(preferencesIds));
if (preferencesIds != null && preferencesIds.length > 0) {
for (int appWidgetId : preferencesIds) {
Log.i(MyWidgetProvider.class.getSimpleName(), "Update widget " + appWidgetId + " from preferences");
updateWidgetId(context, appWidgetManager, appWidgetId);
}
}
if (appWidgetIds != null && appWidgetIds.length > 0) {
for (int appWidgetId : appWidgetIds) {
Log.i(MyWidgetProvider.class.getSimpleName(), "Update widget " + appWidgetId + " from base");
updateWidgetId(context, appWidgetManager, appWidgetId);
}
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
Log.d(MyWidgetProvider.class.getSimpleName(), "Widget with id " + Arrays.toString(appWidgetIds) + "has been deleted");
for (int i = 0; i < appWidgetIds.length; i++) {
MyPreferences.removeWidgetId(context, appWidgetIds[i]);
}
super.onDeleted(context, appWidgetIds);
}
/**
* Update a widget with a specific id
*
* @param context app context
* @param appWidgetManager the widget manager
* @param appWidgetId the widget id to update
*/
private void updateWidgetId(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
MyPreferences.addWidgetId(context, appWidgetId);
Log.d(MyWidgetProvider.class.getSimpleName(), "Widget with id " + appWidgetId + " has been updated");
RemoteViews rv = new RemoteViews(context.getPackageName(),
R.layout.widget);
Intent intent = new Intent(context, StickyWidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
appWidgetId);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
rv.setRemoteAdapter(android.R.id.list, intent);
// Handle click
Intent activityIntent = new Intent(context, NotesListActivity.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, appWidgetId, activityIntent, 0);
rv.setOnClickPendingIntent(R.id.widget_layout, pendingIntent);
// Return
appWidgetManager.updateAppWidget(appWidgetId, rv);
}
}
我们的想法是将所有小部件ID保存在应用程序的共享首选项中,因此,我可以更新它们(通过强制更新意图),例如在我的MainActivity
中,我有一些东西像
/**
* Starts an intent that force the service to wake up and update
* the widgets
*/
private void updateWidgets() {
Intent intent = new Intent(this, MyWidgetProvider.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
// Use an array and EXTRA_APPWIDGET_IDS instead of AppWidgetManager.EXTRA_APPWIDGET_ID,
// since it seems the onUpdate() is only fired on that:
int[] ids = {R.xml.widget_infos}; // As I store the ids, this is kind of useless ?
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
sendBroadcast(intent);
}
但问题是,在logcat中,我得到了正确的东西(更新3个小部件,它正在打印ID),并且在我的屏幕上,只更新了最后一个小部件。
如果有人有线索或......任何东西,那会很棒,因为这让我发疯了:/
答案 0 :(得分:0)
在活动中,您可以以编程方式检索用户主页/锁定屏幕中安装的应用程序相关小部件的所有ID。
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(application);
final int[] smallAppWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(application, SmallWidgetProvider.class));
for (int appWidgetId : smallAppWidgetIds) {
appWidgetManager.updateAppWidget(appWidgetId,
SmallWidgetProvider.buildRemoteView(application, appWidgetId, whatEverYouNeed));
}
AppWidgetManager.updateAppWidget(int, RemoteView)
需要RemoteView
,这就是为什么自定义WidgetProvider
有一个静态方法可以在正在运行的RemoteView
实例之外生成此SmallWidgetProvider
。< / p>
请注意,调用AppWidgetManager.updateAppWidget(int, RemoteView)
不会触发自定义onUpdate()
的{{1}}。
如果您知道每个小部件都具有完全相同的内容,则可以考虑使用AppWidgetProvider
。