Thursday, April 12, 2012

Dynamically control location tracking (registering / unregistering location broadcast receiver). How?

I want to dynamically control location tracking (registering / unregistering location broadcast receiver). This is how I am planning to do it. I have two questions :




  1. What are the mistakes in the implementation below because all this concept is still very theoretical to me as I am very new to android/java dev. Still building concepts!


  2. How do I pass some EXTRA_INFO from my location library class to the location receiver.




IMPLEMENTATION:



I have a library class LocationLibrary.java which consists of two methods. They do as the name suggest. The location tracking should start when I call startTracking(). Plz note the extraInfo that needs to be passed to myLocationReceiver. The tracking should stop when stopTracking() is called.



Code snippet:



public class LocationLibraray
{
private static BroadcastReceiver myLocationReceiver;

public LocationLibraray(Context context)
{
this.ctx = context;
myLocationReceiver = new MyLocationReceiver();
}

public void startTracking(Context context, String extraInfo)
{
IntentFilter filter = new IntentFilter();
filter.addAction("com.app.android.tracker.LOCATION_READY");
context.registerReceiver(myLocationReceiver, filter);

// NEED TO PASS extraInfo to myLocationReceiver for some processing, but HOW?
}

public void stopTracking(Context context)
{
context.unregisterReceiver(locationReceiver);
}


}



MyLocationReceiver.java



public class MyLocationReceiver extends BroadcastReceiver  {

public void onReceive(final Context context, Intent intent) {
if ((intent.getAction() != null) &&
(intent.getAction().equals("com.app.android.tracker.LOCATION_READY")))
{
//GET THAT EXTRA INFO FROM LocationLibrary class and process it here
}
}
}


Please help me out. Thnx!





No comments:

Post a Comment