简体   繁体   English

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

[英]Check if a widget is exists on homescreen using appWidgetId

I am using AlarmManager to update my widgets. 我正在使用AlarmManager来更新我的小部件。 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. 但是我在检测主屏幕上是否没有小部件时遇到问题。

As whenever I try to get the AppWidgetIds using this way: 每当我尝试使用这种方式获取AppWidgetIds时:

AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

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

I get the a length of appWidgetIDs while actually there is no widget on homescreen. 我得到了一段appWidgetIDs而实际上主屏幕上没有小部件。 Why? 为什么?

Therefore, I would like to know if there is a way to detect that a widget id is exists on homescreen. 因此,我想知道是否有办法检测主屏幕上是否存在小部件ID。

Thank you upfront. 先谢谢你。

Congratulations, you've encountered phantom appwidgets. 恭喜你,你遇到了幻影appwidgets。 It appears to be documented on the Android issue tracker . 它似乎记录在Android问题跟踪器上 They usually occur when the configuration activity for an appwidget is canceled, though it seems to be through improper implementation of the configuration activity; 它们通常在取消appwidget的配置活动时发生,尽管它似乎是通过不正确的配置活动实现; developers neglect to include the appwidget ID as an extra when setting the activity result to RESULT_CANCELED . 在将活动结果设置为RESULT_CANCELED时,开发人员忽略将appwidget ID作为额外ID包含。 (even Google's ApiDemos sample application neglects to do this!) (即使Google的ApiDemos示例应用程序忽略了这样做!)

The proper implementation is like 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);
    }
}

Thus far I know of no way to detect the presence of a phantom appwidget without doing your own bookkeeping. 到目前为止,我知道没有办法在没有自己的簿记的情况下检测幻影appwidget的存在。 I suggest storing a SharedPreferences value indicating that the configuration activity was not canceled and then querying this value in your other code. 我建议存储一个SharedPreferences值,表示配置活动未被取消,然后在其他代码中查询该值。 You can also use this information to "delete" a phantom widget if you come across one. 如果遇到虚拟小部件,您还可以使用此信息“删除”虚拟小部件。 In your appwidget configuration activity: 在您的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;
}

Then you can check that you have at least one non-phantom appwidget like so: 然后你可以检查你至少有一个非幻像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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM