Im new to android development and i understand the android activity life cycle. Please see the following code.
public class MyTest extends Activity{
In order to your location question, I would suggest you another new way of retrieving locations.
Check this out: google play util
The code below is just a snippet, not fully ;). You dont need to handle gps/network providers anymore. It handles for you, which one is the best and returns a location.
LocationClient mLocationClient = new LocationClient(...)
mLocationClient.connect();
...
onConnected(Bundle ...){
LocationRequest request = LocationRequest.create();
request.setNumUpdates(1);
...
mLocationClient.requestLocationUpdates(request, new LocationListener....)
....
}
I made one service for that. It is easy for get Longitude / Latitude using it.
Copy/paste this class in your project.
package com.sample;
import com.sample.globalconstant;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MyServiceGPS extends Service
{
private static final String TAG = "BOOMBOOMTESTGPS";
private LocationManager mLocationManager = null;
private static final int LOCATION_INTERVAL = 1000;
private static final float LOCATION_DISTANCE = 10f;
private class LocationListener implements android.location.LocationListener{
Location mLastLocation;
public LocationListener(String provider)
{
Log.e(TAG, "LocationListener " + provider);
mLastLocation = new Location(provider);
}
public void onLocationChanged(Location location)
{
Log.e(TAG, "onLocationChanged: " + location.getLatitude() +"....."+ location.getLongitude());
globalconstant.lat = location.getLatitude();
globalconstant.lon = location.getLongitude();
Toast.makeText(getApplicationContext(), location.getLatitude() +"....."+ location.getLongitude(), 1000).show();
mLastLocation.set(location);
}
public void onProviderDisabled(String provider)
{
Log.e(TAG, "onProviderDisabled: " + provider);
}
public void onProviderEnabled(String provider)
{
Log.e(TAG, "onProviderEnabled: " + provider);
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
Log.e(TAG, "onStatusChanged: " + provider);
}
}
LocationListener[] mLocationListeners = new LocationListener[] {
new LocationListener(LocationManager.GPS_PROVIDER),
new LocationListener(LocationManager.NETWORK_PROVIDER)
};
@Override
public IBinder onBind(Intent arg0)
{
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.e(TAG, "onStartCommand");
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
@Override
public void onCreate()
{
Log.e(TAG, "onCreate");
initializeLocationManager();
try {
mLocationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
mLocationListeners[1]);
} catch (java.lang.SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "network provider does not exist, " + ex.getMessage());
}
try {
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
mLocationListeners[0]);
} catch (java.lang.SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "gps provider does not exist " + ex.getMessage());
}
}
@Override
public void onDestroy()
{
Log.e(TAG, "onDestroy");
super.onDestroy();
if (mLocationManager != null) {
for (int i = 0; i < mLocationListeners.length; i++) {
try {
mLocationManager.removeUpdates(mLocationListeners[i]);
} catch (Exception ex) {
Log.i(TAG, "fail to remove location listners, ignore", ex);
}
}
}
}
private void initializeLocationManager() {
Log.e(TAG, "initializeLocationManager");
if (mLocationManager == null) {
mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
}
}
}
Copy this code in your Activity when you want to start:
startService(new Intent(this,MyServiceGPS.class));
Create one class globalconstant:
public class globalconstant { public static double lat, lon; }
when you want to current latitude and longitude in your project only write this globalconstant.lat ,globalconstant.lon
Add uses-permission in Manifest
LocationClient is the new way of getting GPS. Watch video for complete details on the recent update.
Note that the LocationManager way of doing things is buggy, since there is need to add a lot of code for GPS and NETWORK provider. The new way (internal to Google referred to as the Fused Location Provider) works with sensors to reduce battery consumption big time. Also reduces the need for complex API's and stage wise selection of the best provider. Its just 2-3 lines of code and you are done.
With many Samsung phones (Y model including) though there is a specific issue that most of the times they don't return location at all. So you need to kick start that phone to return the GPS. To do that you can use
HomeScreen.getLocationManager().requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onLocationChanged(final Location location) {
}
});
And then call your locationClient.getLastLocation
api. As in put the code above, just before your LocationClient.getLastLocation or LocationManager.getLastKnownLocation call.
Mind you Samsung is the highly customized android open source product. Google cannot respond to samsung related bugs, and Samsung does not have any android developer related support.
Edit : Watch the video, trust me, without knowing what LocationClient gives you, you wont appreciate the change. You will also learn about GeoFenceing.