JavocSoft Android Toolbox – Location Service – Update

Hi all again,

Today i have had time to isolate a class to have a localization service ready-to-use in the library JavocSoft Android Toolbox.

JavocSoft Android Toolbox LibraryThis will allow, very easily, to have an application that watches for any location change (or GPS enabled/disabled) event in background even if the application is not running because is an Android service 🙂

To use it, first declare in your AndroidManifest.xml:

<service android:name="es.javocsoft.android.lib.toolbox.location.service.LocationService" android:label="Location Service" android:enabled="true"/>

And launch it:

Intent mLocServiceIntent = new Intent(getBaseContext(), LocationService.class);
Bundle extras = new Bundle();
extras.putInt(LocationService.LOCATION_SERVICE_PARAM_MIN_DISTANCE, 2);
extras.putInt(LocationService.LOCATION_SERVICE_PARAM_MIN_TIME, 4000);
mLocServiceIntent.putExtras(extras);
startService(mLocServiceIntent);

And to stop it, when required:

Intent mLocServiceIntent = new Intent(getBaseContext(), LocationService.class);
stopService(mLocServiceIntent);

 

The localization service can also be customized when launched by setting in the service launching intent these parameters:

  • LocationService.LOCATION_SERVICE_PARAM_MIN_DISTANCE
  • LocationService.LOCATION_SERVICE_PARAM_MIN_TIME

Once is running, the service will broadcast the following events:

  • LOCATION_SERVICE_STARTED. Intent filter name:
    es.javocsoft.android.lib.toolbox.location.service.intent.action.LOCATION_SERVICE_STARTED
  • LOCATION_SERVICE_SHUTDOWN. Intent filter name:
    es.javocsoft.android.lib.toolbox.location.service.intent.action.LOCATION_SERVICE_SHUTDOWN
  • LOCATION_CHANGED. Intent filter name:
    es.javocsoft.android.lib.toolbox.location.service.intent.action.LOCATION_CHANGED
  • LOCATION_GPS_DISABLED. Intent filter name:
    es.javocsoft.android.lib.toolbox.location.service.intent.action.LOCATION_GPS_DISABLED
  • LOCATION_GPS_ENABLED. Intent filter name:
    es.javocsoft.android.lib.toolbox.location.service.intent.action.LOCATION_GPS_ENABLED

So now, to attend the localization events you only have to create a BroadcastReceiver:

public class LocationChangedReceiver extends WakefulBroadcastReceiver {
 
 protected static String TAG = "LocationChangedReceiver";
 
 
 public LocationChangedReceiver() {
 
 }
 @Override
 public void onReceive(Context context, Intent intent) {
 
   Bundle bundle = intent.getExtras(); 
   if(intent.getAction().equals(LocationService.ACTION_LOCATION_CHANGED)) { 
     //The Location Service leaves the detected location in the extras using
     //the key LocationService.LOCATION_KEY.
     Location location = bundle.getParcelable(LocationService.LOCATION_KEY);
     //Do something
   }else if(intent.getAction().equals(LocationService.ACTION_LOCATION_GPS_ENABLED)){
     //Do something
   }else if(intent.getAction().equals(LocationService.ACTION_LOCATION_GPS_DISABLED)){
     //Do something
   }else if(intent.getAction().equals(LocationService.ACTION_LOCATION_SERVICE_STARTED))
     //Do something 
   }else if(intent.getAction().equals(LocationService.ACTION_LOCATION_SERVICE_SHUTDOWN)){
     //Do something
   }
 }
}

And declare this receiver in your application AndroidManifest.xml:

<receiver android:name="your_application_package.LocationReceiver" android:enabled="true" android:exported="false"/>
 <intent-filter>
 <action android:name="es.javocsoft.android.lib.toolbox.location.service.intent.action.LOCATION_SERVICE_STARTED"/>
 <action android:name="es.javocsoft.android.lib.toolbox.location.service.intent.action.LOCATION_SERVICE_SHUTDOWN"/>
 <action android:name="es.javocsoft.android.lib.toolbox.location.service.intent.action.LOCATION_CHANGED"/>
 <action android:name="es.javocsoft.android.lib.toolbox.location.service.intent.action.LOCATION_GPS_ENABLED"/>
 <action android:name="es.javocsoft.android.lib.toolbox.location.service.intent.action.LOCATION_GPS_ENABLED"/>
 </intent-filter>
<receiver/>

 

I hope this addition make your life easier 🙂

Do not forget to check the blog to get more updates!

More info and HowTo at https://github.com/javocsoft/javocsoft-toolbox/wiki.

github_icon

As always, library is available on GitHub

javocsoft-toolbox.

Bye.

JavocSoft 2015.

Leave a Reply

Your email address will not be published. Required fields are marked *