当传递RESULT_CANCELED作为结果配置活动的Widget不会被删除结果、RESULT_CANCELED、Widget

2023-09-03 20:38:16 作者:潇洒先森】

我有一些问题,我的小部件。下面是说明:

I have some problems with my widgets. Here is the description:

背景:

我有一个家的小部件。

当我添加它,它会弹出一个配置活动设置一些参数窗口小部件。

When I add it, it pops a configuration Activity for setting some parameters for the widget.

如果我称之为的setResult(RESULT_OK,resultValue); 在完成配置活动之前,小部件添加到主屏幕

If I call setResult(RESULT_OK, resultValue); before finishing the configuration Activity, the widget is added to the Home.

如果我通过拖动到回收站中删除的小部件,公共无效onDeleted从我AppWidgetProvider类被调用(上下文的背景下,INT [] appWidgetIds)。 到目前为止好。

If I delete the widget by dragging it to the trash bin, public void onDeleted(Context context, int[] appWidgetIds) from my AppWidgetProvider class gets called. So far so good.

问题: 如果配置活动将退出,并因此code RESULT_CANCELED(的setResult(RESULT_CANCELED); ),公共无效onDeleted从我AppWidgetProvider类(上下文的背景下,INT [] appWidgetIds)不叫和小部件保持活动部件列表。当我重新启动手机,的onupdate(上下文的背景下,AppWidgetManager appWidgetManager,INT [] appWidgetIds)从我AppWidgetProvider类被称为和INT [] appWidgetIds我把所有部件(的ID),其应该是取消(在添加之前删除)+主动的人(即实际显示在首页的那些)。通过拖动到回收站中删除的小部件不会显示在列表中。随着时间的部件IDS这个名单不断变得越来越大,如果用户是从配置活动取消。

Problem: If the configuration Activity exits with result code RESULT_CANCELED (setResult(RESULT_CANCELED);), public void onDeleted(Context context, int[] appWidgetIds) from my AppWidgetProvider class is not called and the widget remains in the active widgets list. When I restart the phone, onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) from my AppWidgetProvider class is called and in int[] appWidgetIds I have all widgets (the ids) that supposed to be canceled (deleted before being added) + the active ones (the ones that are actually displayed on Home). The Widgets that were deleted by dragging to the trash bin are not displayed in this list. With time this list of widgets ids keeps getting bigger and bigger if the user is canceling from the configuration Activity.

API参考说是这样的: 如果你返回使用RESULT_OK Activity.setResult(),该AppWidget将被添加,并且您将收到一条ACTION_APPWIDGET_UPDATE广播这个AppWidget。如果返回RESULT_CANCELED,主机将取消添加和不显示此AppWidget,您将收到一个ACTION_APPWIDGET_DELETED广播。

The API reference says something like: "If you return RESULT_OK using Activity.setResult(), the AppWidget will be added, and you will receive an ACTION_APPWIDGET_UPDATE broadcast for this AppWidget. If you return RESULT_CANCELED, the host will cancel the add and not display this AppWidget, and you will receive a ACTION_APPWIDGET_DELETED broadcast."

任何人都可以给我一些这方面的提示吗? 谢谢你。

Can anyone give me some hints on this? Thank you.

下面是我的清单:

<application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
    <receiver android:name=".MytWidget" android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <meta-data android:name="android.appwidget.provider"
                    android:resource="@xml/my_widget_provider" />
    </receiver>
    <activity android:name=".ConfigurationActivity">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
        </intent-filter>
    </activity>
</application>

的code,其余是不相关的,因为它是如上所述(我没有权限将它张贴)。

The rest of the code is not relevant since it was explained above (and I don't have permission to post it).

推荐答案

我有同样的问题,我这样做是对的onPause事件

I had this same problem, i did this on the onPause event

public void removeWidget(int appWidgetId) {
    AppWidgetHost host = new AppWidgetHost(Config.this, 1);
    host.deleteAppWidgetId(appWidgetId);
}

经过小部件标识,窗口小部件将被删除。 主机ID并不重要,如果你只有一个应用程序窗口小部件的主机。

Checked the widget ids, the widget is removed. The host id is not important if you only have one app widget host.

private boolean canceled = true;

@Override
protected void onPause() {
    if(canceled) {
        removeWidget(appWidgetId);
    }
    super.onPause();
}

在单击确定,我设置了取消

In the OK click, i set the canceled false