I am trying to get user location either network location or gps
location or both. for that i am running a background service which implements LocationListener
. i added all the require permissions in the manifest file also. here is my service
public class GetLocation extends Service implements LocationListener {
LocationManager lm;
Location networkLocation, gpsLocation;
@Override
public void onCreate() {
super.onCreate();
lm = (LocationManager) getSystemService(LOCATION_SERVICE);
if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
gpsLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Toast.makeText(getApplicationContext(), "Getting Location Via GPS",
Toast.LENGTH_LONG).show();
}
if (lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
this);
networkLocation = lm
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Toast.makeText(getApplicationContext(),
"Getting location Via Network", Toast.LENGTH_LONG).show();
}
if (gpsLocation != null) {
Toast.makeText(
getApplicationContext(),
"GPSLOCATION: " + gpsLocation.getLatitude() + " , "
+ gpsLocation.getLongitude(), Toast.LENGTH_LONG)
.show();
}
if (networkLocation != null) {
Toast.makeText(
getApplicationContext(),
"NETWORKLOCATION: " + networkLocation.getLatitude() + " , "
+ networkLocation.getLongitude(), Toast.LENGTH_LONG)
.show();
}
if (gpsLocation == null && networkLocation == null) {
Toast.makeText(getApplicationContext(),
"Couldn't get user location", Toast.LENGTH_LONG).show();
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
new Thread() {
public void run() {
Looper.prepare();
if (gpsLocation != null) {
Log.d("Usman", "GPSLOCATION: " + gpsLocation.getLatitude()
+ " , " + gpsLocation.getLongitude());
}
if (networkLocation != null) {
Log.d("Usman",
"NETWORKLOCATION: " + networkLocation.getLatitude()
+ " , " + networkLocation.getLongitude());
}
if (gpsLocation == null && networkLocation == null) {
Log.d("Usman", "Couldn't get user lcoation");
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Looper.loop();
}
}.start();
return START_STICKY;
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onLocationChanged(Location location) {
// do something with this location
Toast.makeText(
getApplicationContext(),
location.getProvider() + " Location Changed:"
+ location.getLatitude() + " , "
+ location.getLongitude(), Toast.LENGTH_LONG).show();
}
@Override
public void onProviderDisabled(String arg0) {
Toast.makeText(getApplicationContext(), arg0 + " Disabled",
Toast.LENGTH_LONG).show();
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(getApplicationContext(), provider + " Enabled",
Toast.LENGTH_LONG).show();
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
}
in onCreate() method, both gps and network locations are null. can any body tell me where am i wrong