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.