Question

Scenario 1:

  1. Device is loaded
  2. I add first widget
  3. OneX -> Calls onEnabled | Galaxy Tab -> doesnt call onEnabled.

Scenario 2:

  1. Device is booted with widget already on screen.
  2. OneX -> Calls onEnabled | Galaxy Tab -> Calls onEnabled

But It must be called when user places one on screen (scenario 1), else whats the point. Surely you dont have to restart your device to get onEnabled called.

Its important, because I Attach a alarm manager to update my widget every 2 seconds:

    AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.add(Calendar.SECOND, 1);
    alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), 1000, createClockTickIntent(context));

There is a similar question with no working solution: AppWidgetProvider: not called onEnabled method

There is no need to register these intents, and doing so doesnt help anyway.

My question is why is onEnabled not invoking, and how do I get it to.

It says on google Docs: ACTION_APPWIDGET_ENABLED:

Sent when an instance of an AppWidget is added to a host for the first time. This broadcast is sent at boot time if there is a AppWidgetHost installed with an instance for this provider.

Basically confirming that it should work in both scenarios

This is the intent that triggers onEnabled()

Called in response to the ACTION_APPWIDGET_ENABLED broadcast when the a AppWidget for this provider is instantiated. Override this method to implement your own AppWidget functionality.

The fact it works on the phone, and not the tablet, is strange. Is it an issue with android 3.2 and is there a work around?

Was it helpful?

Solution

After lots of attempt to find why it is acting this way, I came up short.

It is either an android related problem or a device related problem, where the widget enabled broadcast is never sent.

The only work around to get the widget updating with alarm manager, was to initalise the alarm manager from its own method, and then call that from onEnabled AND onUpdate. So when it falls short of onEnabled, it will be covered by onUpdate, when the widget goes to update itself (on app call to it). And in the scenario onEnabled does work, it will just replace the existing alarm manager with a new instance when ever onUpdate is called.

Maybe not the ideal solution depending on what you are trying to do in onEnabled, but the only one I could find.

OTHER TIPS

I also found this quite unreliable, and worked around this problem with broadcast listener for boot complete event. You may also experiment with other callbacks (like onUpdate())

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top