使用appWidgetId检查主屏幕上是否存在小部件

7 android android-widget android-appwidget

我正在使用AlarmManager更新我的小部件.如果主屏幕上没有小部件,我想停止它.但我在检测主屏幕上是否没有小部件时遇到问题.

每当我尝试使用这种方式获取AppWidgetIds时:

AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

int[] appWidgetIDs = appWidgetManager
    .getAppWidgetIds(new ComponentName(context, Widget.class));
Run Code Online (Sandbox Code Playgroud)

我得到了一段appWidgetIDs时间实际上主屏幕上没有小部件.为什么?

因此,我想知道是否有办法检测主屏幕上是否存在小部件ID.

先谢谢你.

Kar*_*uri 18

恭喜你,你遇到了幻影appwidgets.它似乎记录在Android问题跟踪器上.它们通常在appwidget的配置活动被取消时发生,尽管它似乎是通过不正确的配置活动实现; 开发人员忽略了在将活动结果设置为时将appwidget ID作为附加项包含RESULT_CANCELED.(即使Google的ApiDemos示例应用程序也忽略了这一点!)

正确的实现是这样的:

public class AppWidgetConfigActivity extends Activity {

    private int appWidgetId;
    private Intent resultValue;

    protected void onCreate(bundle saved) {
        super.onCreate(saved);

        // get the appwidget id from the intent
        Intent intent = getIntent();
        appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);

        // make the result intent and set the result to canceled
        resultValue = new Intent();
        resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
        setResult(RESULT_CANCELED, resultValue);

        // if we weren't started properly, finish here
        if (appwidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
            finish();
        }

        /* ... */
    }

    /* ... */

    private void finishConfigure() {
        /* finish configuring appwidget ... */
        setResult(RESULT_OK, resultValue);
    }
}
Run Code Online (Sandbox Code Playgroud)

到目前为止,我知道没有办法在没有自己的簿记的情况下检测幻影appwidget的存在.我建议存储一个SharedPreferences值,表示配置活动未被取消,然后在其他代码中查询该值.如果遇到虚拟小部件,您还可以使用此信息"删除"虚拟小部件.在您的appwidget配置活动中:

private void finishConfigure() {
    /* finish configuring appwidget ... */
    setResult(RESULT_OK, resultValue);

    String key = String.format("appwidget%d_configured", appwidgetId);
    SharedPreferences prefs = getSharedPreferences("widget_prefs", 0);
    prefs.edit().putBoolean(key, true).commit;
}
Run Code Online (Sandbox Code Playgroud)

然后你可以检查你至少有一个非幻像appwidget,如下所示:

AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
AppWidgetHost appWidgetHost = new AppWidgetHost(context, 1); // for removing phantoms
SharedPreferences prefs = getSharedPreferences("widget_prefs", 0);
boolean hasWidget = false;

int[] appWidgetIDs = appWidgetManager.getAppWidgetIds(new ComponentName(context, Widget.class));
for (int i = 0; i < appWidgetIDs.length; i++) {
    int id = appWidgetIDs[i];
    String key = String.format("appwidget%d_configured", id);
    if (prefs.getBoolean(key, false)) {
        hasWidget = true;
    } else {
        // delete the phantom appwidget
        appWidgetHost.deleteAppWidgetId(id);
    }
}

if (hasWidget) {
    // proceed
} else {
    // turn off alarms
}
Run Code Online (Sandbox Code Playgroud)