How do I publish an update to Dashclock when my ap

2019-01-27 05:34发布

问题:

I have added a location proximity later using Android's addProximityAlert method in the LocationManager class.

When the user's phone is a particular area, the LocationManager sends an Intent to my application's BroadcastReciver and here is the class that handles the intent:

public class ProximityTrigger extends BroadcastReceiver {

    @Override
    public void onReceive(Context ctxContext, Intent ittIntent) {            
        System.out.println(ittIntent.getIntExtra(WidgetService.KEY_STOP_IDENTIFIER, -1));               
    }

}

I'd like to use the DashClock API to show an update when this happens. DashClock has a publishUpdate method that can be used to show updates but I can only use this from my extension class that extends the DashClock class. This should be a possible somehow but I can't seem to figure out how. Any ideas on how I could accomplish this?

回答1:

Since the extension service must issue the publishUpdate call, your proximity trigger broadcast receiver should use something like a broadcast or an event bus to ask the extension to publish an update.

If the extension isn't initialized, meaning the user hasn't added it or DashClock isn't running, then nothing will happen, which is the expected and correct behavior. As soon as DashClock starts and your extension is added, onUpdateData will be called and you'll be able to publish your update.

For some ideas on the actual classes you can use, see below:

  • Global broadcasts, using Context.sendBroadcast along with a dynamically registered broadcast receiver in your DashClockExtension using Context.registerReceiver.

  • Local broadcasts, using LocalBroadcastManager.

  • Otto or EventBus, two frameworks that let application components publish and subscribe to events. In this case, the extension subscribes and your proximity sensor publishes.

A full code snippet for global broadcasts can be found in issue 292 on the DashClock project page.