简体   繁体   中英

Service is off when screen is turns off

I am confused a little bit on the behavior of the service.I created a service class which is Emailing my location as soon as the latitude and longitude of My Device is changed.The Service is working perfect when the screen of my phone is on but as soon as the phone screen off the service is not updating me for new locations.I have read somewhere on the SO about wake lock.I don't know how can I implement wake lock on my service and even I don't know whether it will work or not.Please guide me how can I acquire wake lock on my service class Code for my service class:

public class LocationUpdater extends Service {
private LocationManager lm;
private LocationListener locationListener;
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
           Toast.makeText(getApplicationContext(), "Location updater started",Toast.LENGTH_LONG).show();
    locationListener = new MyLocationListener();
    lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    if (!lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0,0,
                locationListener);
    } else {
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0, 0,
                locationListener);
        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0, 0,
                locationListener);
    }


    return START_STICKY;
}

@Override
public void onDestroy() {
    lm.removeUpdates(locationListener);
    super.onDestroy();
}
@Override
public void onCreate() {
    Toast.maketext(getApplicationContext,"Updater Serive Created",Toast.LENGTH_LONG).show;


    } 
private class MyLocationListener implements LocationListener {

    @Override
    public void onLocationChanged(Location loc) {

        if (loc != null) {
         String latitude=loc.getLatitude();
         Striing longitude=loc.getLongitude();
         emailMyLocation(latitude,longitude);
         }
        }
       @Override
       public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }
    }
    }

This is the service class which is updating the location of my device to my email.Please tell how can I make it work even if the screen is off.

You should check out answer for this question in the following thread: How can I keep my Android service running when the screen is turned off? . Probably, you need a partial WakeLock, which will keep CPU running even when the screen is turned off. You should also check documentation of PowerManager in Android API, which explains behavior of wake locks.

You can aquire the wake lock like this: Make a Globals class and declare wakelock as static variable

 public class Globals {
          public static PowerManager.WakeLock wakelock;
 }

Now in your service class Declare these:

   PowerManager.WakeLock wakeLock ;
   PowerManager pm;

Now you can have two functions for wakelocks:

public void acquirewakeLock() {
         if(Globals.wakelock!=null){
         Globals.wakelock.release();
         Globals.wakelock=null;
         }
    pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
    "TrackerService");
    wakeLock.acquire();
    Globals.wakelock=this.wakeLock;  
}
public void releaseWakelock() {
      wakeLock.release();
      }

Now in your onCreate method you can quire wakelock with this:

    @Override
    public void onCreate() {
       acquirewakeLock()
     }

This will take care of whether CPU is held by other activities and services or not.And you will be able to give cpu to your sevice class. You can also use

   wakelock.acquire(time in milliseconds)

if you know the time your service needs to process. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM