gpt4 book ai didi

android - AppWidgetManager getAppWidgetIds 返回旧的 Widget id

转载 作者:IT老高 更新时间:2023-10-28 23:24:04 24 4
gpt4 key购买 nike

我正在尝试获取我的小部件的所有 ACTIVE 实例的列表。在我的 AppWidgetProvider 的 OnUpdate 方法中,我正在执行以下操作:

// Get all ids
ComponentName thisWidget = new ComponentName(context, this.getClass());
int[] lastWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

问题在于,如果您将一个小部件添加到主屏幕然后将其删除,getAppWidgetIds 仍会返回一个列表,其中包含您刚刚删除的小部件的 ID。

有没有办法只检索主屏幕上处于 Activity 状态的小部件的 ID?

最佳答案

我也遇到过这个。我猜 getAppWidgetIds 被简单地编程为始终获取与该小部件提供程序关联的所有 ID。为了解决这个问题,我必须将 Widget ID 保存到我的共享首选项中,方法是将 id 保存在 onUpdate 方法中,并在我的 AppWidgetProvider 的 onDelete 方法中删除它。

public class MyProvider extends AppWidgetProvider {
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// Update your widget as normal

// If the Id isn't already saved to your db/app preference, save it now
}

public void onDeleted (Context context, int[] appWidgetIds) {
// Remove each id passed in through appWidgetIds
// "un-save" them from your db or app preferences
}
}

关于android - AppWidgetManager getAppWidgetIds 返回旧的 Widget id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12462696/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com