开发者

AppWidget click lost after system restarts my process

开发者 https://www.devze.com 2023-02-25 14:10 出处:网络
i am making an appwidget and i have problems with click event, which is lost when system kills the widget\'s process and later restarts it. this also happens after screen rotate.

i am making an appwidget and i have problems with click event, which is lost when system kills the widget's process and later restarts it. this also happens after screen rotate.

building against SDK version 7 and running on emulator (2.1) and a real device with 2.3.3.

my onUpdate method:

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    for (int wid : appWidgetIds) {
        L开发者_运维技巧og.i(TAG, "onUpdate widget #" + wid);

        Intent intent = new Intent(context, MyClass.class);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, wid);

        PendingIntent clickIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        RemoteViews widget = new RemoteViews(context.getPackageName(), R.layout.widget);
        widget.setOnClickPendingIntent(R.id.widget_layout, clickIntent);

        appWidgetManager.updateAppWidget(wid, widget);
    }

    super.onUpdate(context, appWidgetManager, appWidgetIds);
}

where R.id.widget_layout is id of linear layout of the appwidget. i tried to add this click event also to a textview, but with same result.

i am fighting this problem for several days and i found some people with this same problem, but no solution works for me :( i also tried different pending intent flags without any success.

second problem is, when i add another appwidget on home screen, it does not react to click events. in logcat i see the message from onUpdate method "onUpdate widget #xy", but the appwidget does not react to clicks. only the first appwidget placed on home screen reacts to clicks, but only for some time. any ideas?


When you say the first widget stops responding to clicks, do you mean that the onUpdate method is not being called? Perhaps put some code in onEnabled(Context context) to see if that's being called instead, and if so, put whatever logic is necessary in that function. Also, you can catch intents via the onReceive method (found at the same link) to see which ones your widget is actually receiving.

Also, ensure that the Context that you have the receiver running in (the one that gets passed to this function) is an Application or Service, and not an Activity, or the reference to it may not persist.

You must also make sure that every time you update the widget with a RemoteViews object, you send all of the data needed to fully reconstruct the widget. This is because on, e.g., screen rotation, the system doesn't have anything to reconstruct the widget with beside the very latest RemoteViews you passed it.

0

精彩评论

暂无评论...
验证码 换一张
取 消