Hardik Mehta
Hardik Mehta

Reputation: 43

request location updates not working during device sleep

I am using location manger class to receive location updates my requirement is such that I have to listen for contious location updates but the problem I am facing that once it disconnects I don't know how to reatablish GPS connection,furthermore in some device once device sleeps i m not able to receive any location updates please provide any solutions to achieve this any help is appreciated..

public void setup(MainActivity activity) {
        if (!setup) {
            this.activity = activity;
            Intent locationIntent = new Intent(this.activity, LocationIntentService.class);
            mLocationPendingIntent = PendingIntent.getService(this.activity, 0, locationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            Intent detectedIntent = new Intent(this.activity, ActivityDetectionIntentService.class);
            mDetectedActivityPendingIntent = PendingIntent.getService(this.activity, 0, detectedIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            googleApiClient = new GoogleApiClient.Builder(activity)
                    .addConnectionCallbacks(activity)
                    .addOnConnectionFailedListener(activity)
                    .addApi(ActivityRecognition.API)
                    .build();
            setup = true;
        }
    }
**LocationIntentService.java**
public class LocationIntentService extends IntentService {


    public LocationIntentService() {
        super("LocationServices");
    }

    public LocationIntentService(String name) {
        super("LocationServices");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent != null) {
            Location location = intent.getParcelableExtra(LocationManager.KEY_LOCATION_CHANGED);
            if (location != null) {
                Intent localIntent = new Intent(HWUtil.LOCAL_RECEIVER);
                localIntent.addCategory(Intent.CATEGORY_DEFAULT);
                localIntent.putExtra(HWUtil.LOCATION, location);
                localIntent.putExtra(HWUtil.FROM, HWUtil.LOCATION_SERVICE);
                sendBroadcast(localIntent);
            }
        }
    }

}

and i m sending this location updates to Broadcastrecciver

Upvotes: 2

Views: 1770

Answers (3)

gkovacs
gkovacs

Reputation: 11

I tried to add this code to my project in my MainActivity/onCreate void:

        PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
        PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                "MyApp::MyWakelockTag");
        wakeLock.acquire();

Then I tested it my tablet. I was working over ~25 mins but then it stopped again. Sending of the location data was stopped. Then I pushed the pwr button of the tablet and the GPS ikon disapeared on the top of the screen and did not come back...

Upvotes: 0

Android Enthusiast
Android Enthusiast

Reputation: 4950

AFAIK, wake lock is the easiest way to do. PowerManager.WakeLock

wake lock is a mechanism to indicate that your application needs to have the device stay on.

Any application using a WakeLock must request the android.permission.WAKE_LOCK permission in an element of the application's manifest. Obtain a wake lock by calling newWakeLock(int, String).

Call acquire() to acquire the wake lock and force the device to stay on at the level that was requested when the wake lock was created.

You should aquire a wake lock:

//in onCreate of your service
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
cpuWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"gps_service");
cpuWakeLock.acquire();

// Release in onDestroy of your service
if (cpuWakeLock.isHeld())
cpuWakeLock.release();
and add this to your manifest:

https://developer.android.com/reference/android/Manifest.permission.html Above is good if you need continuous location updates.

Upvotes: 0

solver
solver

Reputation: 56

Please note that the continuous usage of pure GPS as location provider is quite energy hungry on mobile devices. Once that is said, I would perform your task as follows:

  1. I would use a (background) service that would be working togheter with your mobile app. I.e., the mobile app will start the execution of this service (check startForeground() functionality so that your service could be run almost with no interruption, and including a notification on statusBar that can be linked to your activity).
  2. The service (or any internal class) would implement the LocationListener interface and will be the one that actually will be sampling locations. Once you get a location you will process it (depending on your requirements you might want to process it in another thread since the default thread of a created service is the same than the Activity UI).
  3. Once processed, youw would deliver the response to the Android activity, i.e., you would call a public method of your activity, or would implement a more complex communication strategy with Handlers, etc.
  4. In regard with the continuous location sampling, I would strongly suggest you to use AlarmManager services so that you could schedule the next readings (you could make it at exact repeating intervals, see official developer's documentation).
  5. If (and only if) the processing of the location update is heavy or time consuming (for instance, you have to transmit it to a server) you could acquire and hold a WakeLock for avoiding the device to fall into sleep mode; do not forget to release the WakeLock after your processing is done, since it is a major cause of energy sinks in mobile apps, so be very careful with this.

Hope it helps for you

Upvotes: 3

Related Questions