Get Gps Location via a Service in Android

Use service to get GPS location, android?

The best way would be to use location services by CWAC
Location Poller service is already made for us to use it just you have to give the time interval to wake up it

Do it like dis way n you'll need the jar file which you can get it from https://www.dropbox.com/sh/pgxk2v9l5vl0h2j/3svyZnuwOK/CWAC-LocationPoller.jar

From your activity start LocationPoller and set the alarm repeating to the time you want

AlarmManager alarmMgr = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent i = new Intent(this, LocationPoller.class);

Bundle bundle = new Bundle();
LocationPollerParameter parameter = new LocationPollerParameter(bundle);
parameter.setIntentToBroadcastOnCompletion(new Intent(this,
LocationReceiver.class));
// try GPS and fall back to NETWORK_PROVIDER
parameter.setProviders(new String[] { LocationManager.GPS_PROVIDER,
LocationManager.NETWORK_PROVIDER });
parameter.setTimeout(120000);
i.putExtras(bundle);

PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime(), 300000, pi);

Make a receiver class Location Receiver from where you'll fetch the lat n lon

public class LocationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {


try {
Bundle b=intent.getExtras();

LocationPollerResult locationResult = new LocationPollerResult(b);

Location loc=locationResult.getLocation();
String msg;

if (loc==null) {
loc=locationResult.getLastKnownLocation();

if (loc==null) {
msg=locationResult.getError();
}
else {
msg="TIMEOUT, lastKnown="+loc.toString();
}
}
else {
msg=loc.toString();



Log.i("Location Latitude", String.valueOf(loc.getLatitude()));
Log.i("Location Longitude", String.valueOf(loc.getLongitude()));
Log.i("Location Accuracy", String.valueOf(loc.getAccuracy()));




}

Log.i(getClass().getSimpleName(), "received location: " + msg);



}
catch (Exception e) {
Log.e(getClass().getName(), e.getMessage());
}
}

and add this to your manifest

   <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

<receiver android:name="com.commonsware.cwac.locpoll.LocationPoller" />

<service android:name="com.commonsware.cwac.locpoll.LocationPollerService" />

and the receiver declaration where you'll fetch everything

   <receiver android:name="com.RareMediaCompany.Helios.LocationReceiver" />

How to use GPS Service in android

You can use a service for background process.

For your question for getting location, you should try these links:

Best way to get user GPS location in background in Android

Get GPS location via a service in Android

getting location via GPS on android with Processing

The solution:

  • change the "0, 0" in locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); to a "1000, 0" depending on how often you want to get your current position in this case: 1000 ms. Im not gonna talk about the second 0.

  • also instead of using NETWORK_PROVIDER use GPS_PROVIDER to get your true GPS pos instead of networked pos.



Related Topics



Leave a reply



Submit