1

I would like to know there is a way to get location if only it changes? I know android provides this http://developer.android.com/training/location/receive-location-updates.html to get location updates and as it states:

"If your app does navigation or tracking, you probably want to get the user's location at regular intervals. While you can do this with LocationClient.getLastLocation(), a more direct approach is to request periodic updates from Location Services. In response, Location Services automatically updates your app with the best available location, based on the currently-available location providers such as WiFi and GPS."

How to do it without using regular intervals? The main goal is to minimize power consumption. For example, if i am in a room and sitting, there will be no location change. If i start moving, it should report the location changes.

I do need to get locations if only update, so not constantly.

The method should run on the background continuously and provide the all location updates in a power efficient way.

I found android fused location API here https://developer.android.com/google/play-services/location.html

How about using this?

Can i implement it without using regular intervals?

FalconEyes
  • 11
  • 1
  • 3

4 Answers4

0
 protected void updateNotification() {
         LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
         LocationListener locationListener = new MyLocationlistener();

         lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, normallocationwait, 0.250f, locationListener);
         location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
   }

   private class MyLocationlistener implements LocationListener {

     public void onLocationChanged(Location location){
         if(location!=null){
             if(location.hasAccuracy()){
                   dumpLocation(location);
             }else{
                   dumpLocation(location);
            }
         } 
     }

     public void onProviderDisabled(String provider){
         Log.v("Loc Update","\nProvider disabled: " + provider);
     }

     public void onProviderEnabled(String provider){
         Log.v("Loc Update","\nProvider enabled: " + provider);
     }

     public void onStatusChanged(String provider, int status, Bundle extras){
         Log.v("Loc Update","\nProvider status changed: " + provider + ", status="
                    + status + ", extras=" + extras);
     }
Kanaiya Katarmal
  • 5,974
  • 4
  • 30
  • 56
0
// try this way
// declaration
public static LocationManager mlocManager;
public static LocationListner mListner;
private static String latitude;
public static String longitude;

//initialization
try {
    if (mlocManager == null)
            mlocManager = (LocationManager) getSystemService(Activity.LOCATION_SERVICE);
    if (mListner == null)
            mListner = new LocationListner();
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    try {
                        try {
                            mlocManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 0, 0, mListner);
                        } catch (Throwable e) {
                            e.printStackTrace();
                        }

                        mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mListner);
                    } catch (Throwable e) {
                        e.printStackTrace();
                    }
                    try {
                        mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mListner);
                    } catch (Throwable e) {
                        e.printStackTrace();
                    }

                }
            });
} catch (Throwable e) {
    e.printStackTrace();
}

//listener
class LocationListner implements LocationListener {

        @Override
        public void onLocationChanged(Location location) {
            setLatitude("" + location.getLatitude());
            setLongitude("" + location.getLongitude());
            setCurrentLocation(location);
        }

        @Override
        public void onProviderDisabled(String provider) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }

}
// Setter
public void setLatitude(String latitide) {
    this.latitude = latitide;
}
// Getter
public String getLatitude() {

    try {
        if (latitude != null) {
            return latitude;
        }
        if(mlocManager==null)
        {
            mlocManager = (LocationManager) getSystemService(Activity.LOCATION_SERVICE);
        }
        Location loc = mlocManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (loc == null) {
            loc = mlocManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            if (loc == null) {
                loc = mlocManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
            }
            if (loc != null) {
                    return "" + loc.getLatitude();
            }
            } else {
                    return "" + loc.getLatitude();
            }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "0";
}
  // Setter
public void setLongitude(String longitude) {
    this.longitude = longitude;
}
// Getter
public String getLongitude() {
    try {
        if (longitude != null) {
            return longitude;
        }
        if(mlocManager==null)
        {
            mlocManager = (LocationManager) getSystemService(Activity.LOCATION_SERVICE);
        }
        Location loc = mlocManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (loc == null) {
            loc = mlocManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            if (loc == null) {
                loc = mlocManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
            }
            if (loc != null) {
                return "" + loc.getLongitude();
            }
            } else {
                return "" + loc.getLongitude();
            }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "0";
}
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
0

I've searched for this article so many times! and I've found something like this but it's not pretty accurate although maybe this strategy help you, but I didn't found anything useful unless the other answers that friends gave you, so here's how I use it, It called PhoneStateListener :

first you need to add the permissions:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
public class LocationService extends PhoneStateListener {
    private TelephonyManager telephonyManager;
    private boolean isLocationChanged;

    public LocationService(Context context) {
        telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

        if (telephonyManager != null){
            isLocationChanged= telephonyManager.isNetworkRoaming();
            telephonyManager.listen(this,PhoneStateListener.LISTEN_CELL_LOCATION);
        }
    }

    public boolean locationChanged(){
        return isLocationChanged;
    }

    void unregisterListener(){
        telephonyManager.listen(this,PhoneStateListener.LISTEN_NONE);
    }

    @Override
    public void onCellLocationChanged(CellLocation location) {
        if (location != null){
            isLocationChanged = true;
        }
    }
}

according to the documentations:

A listener class for monitoring changes in specific telephony states on the device, including service state, signal strength, message waiting indicator (voicemail), and others.

Override the methods for the state that you wish to receive updates for, and pass your PhoneStateListener object, along with bitwise-or of the LISTEN_ flags to TelephonyManager#listen. Methods are called when the state changes, as well as once on initial registration.

I'm not pretty sure of it's work yet, so please let me know if it works fine for you! :)

here is another example : Add PhoneStateListener

Sina
  • 87
  • 1
  • 12
-1

This method requires GooglePlayService, and it doesn't need much power, maybe you can try it.

private LocationClient locationClient;
private static final int LOCATION_UPDATES_INTERVAL = 10000;
private static final int LOCATION_NUM_UPDATES = 5;

public void startLocationUpdate() {
    if (!(locationClient.isConnected() || locationClient.isConnecting())) {
        locationClient.connect(); // Somehow it becomes connected here
        return;
    }
    // Request for location updates
    LocationRequest request = LocationRequest.create();
    request.setInterval(LOCATION_UPDATES_INTERVAL);
    request.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
    request.setNumUpdates(LOCATION_NUM_UPDATES);
    locationClient.requestLocationUpdates(request, this);
}

And in the Activity you should implement the following callback

private Location mLastLocation;
    @Override
public void onLocationChanged(Location location) {
    if (location == null) {
        Log.v(TAG, "onLocationChanged: location == null");
        return;
    }

    if (mLastLocation != null &&
            mLastLocation.getLatitude() == location.getLatitude() &&
            mLastLocation.getLongitude() == location.getLongitude()) {
        return;
    }
    LogUtil.i(TAG, "LocationChanged == @" +
            location.getLatitude() + "," + location.getLongitude());
    mLastLocation = location;
}
Fate Chang
  • 197
  • 3