6

I have service that is implementing location listener. Now my question is how to make sure that my service will capture location even in sleep mode. I have read about alarm manager

alarm.setRepeating(AlarmManager.RTC_WAKEUP, triggerAtMillis, intervalMillis, operation);

but how to use it. here is my code.. any help would be appreciated..
my service

public class LocationCaptureService extends Service implements LocationListener {
public static int inteval;
java.sql.Timestamp createdTime;
LocationManager LocationMngr;

@Override
public void onCreate() {
    inteval=10*1000;

    startLocationListener(inteval);
}

@Override
public IBinder onBind(Intent arg0) {
    return null;
}

private void startLocationListener(int inteval,String nwProvider) {
    this.LocationMngr = (LocationManager)getSystemService(Context.LOCATION_SERVICE);        
        this.LocationMngr.requestLocationUpdates(LocationManager.GPS_PROVIDER, inteval, 0, this);
}   

public void onLocationChanged(Location location) {
    String status="c",S=null;
    double longitude,lattitude,altitude;
    g_currentBestLocation = location;
    createdTime = new Timestamp (new java.util.Date().getTime());    
         longitude=location.getLongitude();
         lattitude=location.getLatitude();
         altitude=location.getAltitude();
         //use this
         }
  }                              
  public void onProviderDisabled(String provider) {}                            
  public void onProviderEnabled(String provider) {}                             
  public void onStatusChanged(String provider, int status, Bundle extras) {}

}

  • I don't think you want to use `AlarmManager` unless you are needing to schedule specific intervals to query for location. If you want your service not to be killed until a) your app is done b) the user tells your app to stop, then you need a different approach. – Brett Duncavage Apr 11 '13 at 07:15
  • thanx for the comment.. can u please tell me how to make service no to be killed until my app is done. do i need to return start_sticky –  Apr 11 '13 at 07:18

1 Answers1

5

If you want to ensure that your service is not killed/reclaimed by the OS you need to make it a foreground service. By default all services are background, meaning they will be killed when the OS needs resources. For details refer to this doc

Basically, you need to create a Notification for your service and indicate that it is foreground. This way the user will see a persistent notification so he knows your app is running, and the OS will not kill your service.

Here is a simple example of how to create a notification (do this in your service) and make it foreground:

Intent intent = new Intent(this, typeof(SomeActivityInYourApp));
PendingIntent pi = PendingIntent.getActivity(this, 0, intent,   PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

builder.setSmallIcon(Resource.Drawable.my_icon);
builder.setTicker("App info string");
builder.setContentIntent(pi);
builder.setOngoing(true);
builder.setOnlyAlertOnce(true);

Notification notification = builder.build();

// optionally set a custom view

startForeground(SERVICE_NOTIFICATION_ID, notification);

Please note that the above example is rudimentary and does not contain code to cancel the notification, amongst other things. Also, when your app no longer needs the service it should call stopForeground in order to remove the notification and allow your service to be killed, not doing so is a waste of resources.

Brett Duncavage
  • 2,163
  • 14
  • 16
  • i tried this approach on the Huawei U8860 and the service is not working while the device is in the sleep mode ( screen is off and the device is locked). and this problem is only exists with this Huawei U8860, i've tried it on the HTC ONE X , S3 , Experia T, S2, ... and it works. Any ideas about why this is not working with this device ?? thanks – Houcine Sep 25 '13 at 10:59
  • A year later, and I'm replying to this comment, better late than never I suppose. You may need to hold a WakeLock in order for your service to keep running. See https://developer.android.com/reference/android/os/PowerManager.WakeLock.html for more info. – Brett Duncavage Sep 26 '14 at 16:48
  • 1
    a year and a day :D . – Houcine Sep 26 '14 at 20:21