2

I have a location service. Time to time when the service receive new location i send all the past locations accumulated to the server. I don't really know how wake lock work under android, so do i need to keep a wake lock (or any think else?) when i send the data to the server (I send it in background thread if it's matter)

this is the code of my service if it's matter.

public class mLocationService extends Service implements ALLocationServicesListener {


  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {

    if ((intent != null) && (intent.hasExtra("Alarm"))) { synchLocations(); }
    else { 

      return Service.START_STICKY; 

    }
  }


  private class doSynchLocationsTask extends AsyncTask<JSONObject, Void, Boolean> {

    @Override
    protected Boolean doInBackground(JSONObject... params) {

      //send the data in http
      return true;

    }

    @Override
    protected void onPostExecute(Boolean result) {

    }

  }

  private void doSynchLocations(){

    new doSynchLocationsTask().execute(null);

  }

}

1 Answers1

3

If you are using WakefulBroadcastReceiver just call:

  boolean completeWakefulIntent (Intent intent)

You can also use android-job library for job scheduling. This way you don't have to worry about wake locks.

Rade
  • 310
  • 2
  • 15
  • I m just inside a myservice that extends Service –  Jul 29 '17 at 17:09
  • i update my question with the squeleton on my service –  Jul 29 '17 at 17:14
  • You don't need wake lock for service that you posted. As I can see, your app will have great impact on device battery life. Based on this you should think about using mechanism to register repeating alarm which will trigger your action (send data to server). Read this [post](https://stackoverflow.com/a/14377875/2960949). And once again read about android-job and try to find out if it fits your needs. – Rade Jul 29 '17 at 21:16
  • Thanks Rade. But can you explain me why i will not need a wake lock inside doSynchLocations? and in with scenario i need it ? –  Jul 30 '17 at 07:23
  • I didn't see that you explicitly acquire wake lock, so you don't have to release them. With wake locks you have to be very careful. It prevents device from going to sleep. If your goal is to keep device awake than you can do this by using [wake lock] (https://developer.android.com/training/scheduling/index.html). I suggest you to think about another approach to your problem. Think if you can achieve same results by scheduling alarms that will be triggered at specific time. – Rade Jul 30 '17 at 08:19