0

At first i thought that it would work as is, due to extending BroadcastReciever, however it does not.

So onEnabled I have registered it for the desired intent ACTION_TIME_TICK.

However, it is never received.

How can I correctly capture the broadcast, so that i can update the widget's clock, as well as refresh some values on my widget. Doing so on the minute mark is the ideal time.

public class Widget extends AppWidgetProvider {
    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        Log.v("WIDGET", "Recieved: " + intent.getAction());
    }

    @Override
    public void onDisabled(Context context) {
        super.onDisabled(context);
        //Turn off auto update
        context.unregisterReceiver(this);
    }

    @Override 
    public void onEnabled(Context context) {
        super.onEnabled(context);

        context.registerReceiver(this, new IntentFilter(Intent.ACTION_TIME_TICK));
        // Start auto update on Enable (widget exists)
    }

}
IAmGroot
  • 13,760
  • 18
  • 84
  • 154

1 Answers1

2

Your code should be crashing, as you cannot register a BroadcastReceiver from the Context supplied to a BroadcastReceiver via onReceive(). AppWidgetProvider is a subclass of BroadcastReceiver; methods like onUpdate() are called from its onReceive() method.

Even if this somehow no longer crashes like it used to, it will not work reliably. Your process can be terminated in milliseconds after onUpdate() returns.

You are welcome to add this Intent action to your <intent-filter> for your Widget. Just bear in mind that when you override onReceive(), if the action is not one that you are handling yourself, chain to the superclass, so onUpdate() and kin can be called.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Hey thanks for the reply. It doesn't crash for me at least (4.2.2), but nothing happens. I have `` inside my `reciever`s `intent-filter`. But i read from your answer here http://stackoverflow.com/a/19225701/940834 that it is pointless. Hence how i came to register broadcast instead. Unsure how to get this working. Should i be removing the registers? – IAmGroot Feb 17 '16 at 13:43
  • 1
    @Doomsknight: Ah, I forgot that you can't register for that action via the manifest. I apologize for that. In short, there is no good way for you to do what you are trying to do. The list of bad ways includes `AlarmManager` and an everlasting service (where the service has the `registerReceiver()` code that you are trying to use in your `AppWidgetProvider`). – CommonsWare Feb 17 '16 at 13:48
  • I have `AlarmManager` code working previously (wrote a few years ago). It fires at every 5 seconds, to ensure it updates at or just after the minute mark. (so the minute shows correctly on clock). I was looking to improving battery usage. I will revert my code to that, based on your suggestions. And see if I can get it to fire just after the minute instead. Thanks for your help :) – IAmGroot Feb 17 '16 at 13:51
  • 1
    @Doomsknight: You're probably already aware of this, but bear in mind that your code will not run on time on Android 6.0+ devices in Doze mode or if your app is in app standby mode. – CommonsWare Feb 17 '16 at 13:52
  • I was not aware. Do you have any info i can read up on please. I can fire broadcasts from the app. To update the widget as required. It is just the clock that is a pain.. I thought `DigitalClock` would be a built in clock that changes automatically. But it just doesnt render my widget if i use it. – IAmGroot Feb 17 '16 at 13:55
  • Found some :) http://developer.android.com/training/monitoring-device-state/doze-standby.html. I might just remove the clock. despite liking it – IAmGroot Feb 17 '16 at 13:57