Android onEnabled/onDisabled never called

2020-07-13 10:22发布

I'm having a problem with triggering of the onEnabled and onDisabled methods of the AppWidgetReciever. I have the actions specified, I also log every action in onRecieve method, but the only two actions I get are "APPWIDGET_UPDATE" and "APPWIDGET_DELETED". I have googled for this solution, but unfortunately found none. Here is my code:

Manifest part:

<receiver android:name=".ScheduleWidgetProvider" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_ENABLED" />
                <action android:name="android.appwidget.action.APPWIDGET_DISABLED" />
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                <action android:name="android.appwidget.action.APPWIDGET_DELETED" /> 
            </intent-filter>
            <meta-data android:name="android.appwidget.provider"
                android:resource="@xml/provider_xml" />
        </receiver>

Provider_xml:

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="180dp"
    android:minHeight="40dp"
    android:updatePeriodMillis="60000"
    android:initialLayout="@layout/widget_layout"
    android:configure="org.zivkovic.schedule.ConfigurationActivity"
    >
</appwidget-provider>

ScheduleWidgetProvider:

public class ScheduleWidgetProvider extends AppWidgetProvider {
    {
        Log.w("Schedule", "ScheduleWidgetProviderRequested");
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.w("Schedule", "On recieve " + intent.getAction());
        super.onReceive(context, intent);
    }

    @Override
    public void onEnabled(Context context) {
        Log.w("Schedule", "OnEnabled called");
        ...
        super.onEnabled(context);
    }

    @Override
    public void onDisabled(Context context) {
        Log.w("Schedule", "OnDisabled called");

        ...

        super.onDisabled(context);
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        for (int i = 0; i < appWidgetIds.length; i++) {
            ....
        }
        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }

}

Any help is appreciated. Thanks!

2条回答
别忘想泡老子
2楼-- · 2020-07-13 10:56

I know this is an old post, but it seems that the problem is the onReceive(Context context, Intent intent) method.

As it appears, it takes precedence in handling receiver's events, preventing AppWidgetProvider's designated methods from being called upon receiving action.

There are two possible solutions; either handle intent's action in the onReceive method, or remove the method and override the designated action methods.

Do not forget to include the actions in the intent-filter.

查看更多
我只想做你的唯一
3楼-- · 2020-07-13 11:03

Okay, removing my app from the phone did the trick. Can't understand why uninstall fixed it. With anyone having the same problem, uninstall and reinstall your app.

查看更多
登录 后发表回答