检查一个小部件使用appWidgetId存在于主屏幕 [英] Check if a widget is exists on homescreen using appWidgetId

查看:367
本文介绍了检查一个小部件使用appWidgetId存在于主屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 AlarmManager 来更新我的小部件。我想阻止它,如果有主屏幕上没有部件。但我现在面临一个问题,检测是否有在主屏幕上没有任何部件。

I am using AlarmManager to update my widgets. And I want to stop it if there is no widget on homescreen. But I am facing a problem with detecting if there is no widget on home screen.

由于每当我试图用这种方式来获得AppWidgetIds:

As whenever I try to get the AppWidgetIds using this way:

AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

int[] appWidgetIDs = appWidgetManager
    .getAppWidgetIds(new ComponentName(context, Widget.class));

我得到的 appWidgetIDs 的长度,而实际上没有对主屏幕无插件。 为什么?

I get the a length of appWidgetIDs while actually there is no widget on homescreen. Why?

所以,我想知道是否有一种方法来检测一个小部件ID是存在于主屏幕。

Therefore, I would like to know if there is a way to detect that a widget id is exists on homescreen.

感谢您的前期。

推荐答案

恭喜你,你遇到幻象appwidgets。它似乎被记录在 Android的问题跟踪。它们通常发生在一个appwidget配置活动被取消,但它似乎是通过不正当的实现配置活动的;开发商忽略了包括appwidget ID作为一个额外的设置活动结果 RESULT_CANCELED 时。 (即使是谷歌的ApiDemos示例应用程序忽略做到这一点!)

Congratulations, you've encountered phantom appwidgets. It appears to be documented on the Android issue tracker. They usually occur when the configuration activity for an appwidget is canceled, though it seems to be through improper implementation of the configuration activity; developers neglect to include the appwidget ID as an extra when setting the activity result to RESULT_CANCELED. (even Google's ApiDemos sample application neglects to do this!)

的正确实施是这样的:

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);
    }
}

到目前为止,据我所知,没有办法检测出幻影appwidget的presence没有做你自己记账。我建议存储共享preferences值,表明配置活性并没有取消,然后查询在其他code此值。您也可以使用这些信息来删除幻影小工具,如果你遇到之一。在您的appwidget配置活动:

Thus far I know of no way to detect the presence of a phantom appwidget without doing your own bookkeeping. I suggest storing a SharedPreferences value indicating that the configuration activity was not canceled and then querying this value in your other code. You can also use this information to "delete" a phantom widget if you come across one. In your appwidget configuration activity:

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;
}

然后你可以检查你至少有一个非虚appwidget像这样:

Then you can check that you have at least one non-phantom appwidget like so:

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
}

这篇关于检查一个小部件使用appWidgetId存在于主屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆