FiD*_*iDo 19 android android-widget
我的小部件有些问题.这是描述:
语境:
我有一个家庭小部件.
当我添加它时,它会弹出一个配置Activity来设置窗口小部件的一些参数.
如果我调用setResult(RESULT_OK,resultValue); 在完成配置Activity之前,窗口小部件将添加到Home.
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.
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.
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>
其余的代码是不相关的,因为它已在上面解释(我没有权限发布它).
tsu*_*imi 13
我有同样的问题,我在onPause事件上做了这个
public void removeWidget(int appWidgetId) {
    AppWidgetHost host = new AppWidgetHost(Config.this, 1);
    host.deleteAppWidgetId(appWidgetId);
}
检查小部件ID,小部件被删除.如果您只有一个应用小部件主机,则主机ID并不重要.
private boolean canceled = true;
@Override
protected void onPause() {
    if(canceled) {
        removeWidget(appWidgetId);
    }
    super.onPause();
}
在OK单击中,我设置了canceledfalse
您确定您的代码不相关吗?你的清单中的所有东西都不在书中,看起来很好.您的代码应该与此非常相似:
    public void configCancelOnClick(View v) {
    MyLog.d(TAG, "configCancelOnClick");
    Intent intent = new Intent();
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    setResult(RESULT_CANCELED, intent);
    finish();
}
putExtra需要告诉操作系统你没有创建哪个小部件...需要完成才能正常关闭.如果您没有它,您将无法正确传递给操作系统的意图并丢失setResult.
最后,当config没有正确退出(后退键,主页键或坏代码)时,会创建一个ghost小部件.即使使用完美的代码,如果用户在配置中点击主页键,您将有一个排队到系统的小部件,这个小部件在任何主屏幕上都不存在.这就是我称之为幽灵的原因.一旦窗口小部件成功完成配置,它将在从主屏幕移除时调用onDeleted.你有一个问题,如果鬼已经创建,onDisabled将永远不会运行.
最后一次检查.自配置运行以来,您的info xml文件中包含以下内容.但为了以防万一,它看起来像这样:
    android:configure=your.package.name.ConfigurationActivity"
| 归档时间: | 
 | 
| 查看次数: | 3630 次 | 
| 最近记录: |