0

I have developed an android application which runs background service, I need the service to stay alive even if the application is killed by user or for some reason. I have implemented the service and add return START_STICKY in onStartCommand method like this:

public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);

    Log.d("my_state","On start command");

    return START_STICKY;
}

also I defined the service in AndroidManifest.xml like this:

    <service android:name=".MeterDistanceCalcService"
        android:exported="true"
        android:enabled="true"
        android:stopWithTask="false"
        >

        <intent-filter>
            <action android:name="LONGRUNSERVICE" />
        </intent-filter>

    </service>

I noticed that on old android versions, like android 4.4.2, it works fine and the services stays alive, but on some devices which has android 6.0, it does not work. The device is Huawei,

-Settings -> apps -> (MY APP) -> Battary

I found permission "Keep running after screen off" when I toggle this permission to on, it start working as I want. how can I give this permission or whatever to my application, so the service will not be killed?

enter image description here

Community
  • 1
  • 1
Omar Taha
  • 97
  • 1
  • 11

1 Answers1

1

Developer's documentation says you can achieve this behaviour by using the attribute:

 android:isolatedProcess="true"

OR

 android:stopWithTask="false"

OR Using Both

START_STICKY will be ignored if the resources are scare.

By using android:stopWithTask="false" wont allow tasks to be closed even app is destroyed

eg:

<service
        android:name=".ServiceName"
        android:process=":MYPROCESS"
        android:isolatedProcess="true"
        android:stopWithTask="false">
            <intent-filter>
                 <action android:name="PackageName.ServiceName" />
            </intent-filter>
  </service>
Tomin B Azhakathu
  • 2,656
  • 1
  • 19
  • 28
  • What do you mean by "By using android:stopWithTask="false" wont allow tasks to be closed even app is destroyed" ? I didn't got, could you please clarify? – Omar Taha Dec 14 '17 at 11:59
  • Add these to the manifest. On seeing these the service will work as an independent task and the service won't be destroyed even app is destroyed until the Service is stoped manually or fone is Rebooted – Tomin B Azhakathu Dec 14 '17 at 12:07
  • 1
    When I added the attribute android:isolatedProcess="true" the service does not start. – Omar Taha Dec 14 '17 at 12:29
  • I managed to run the service, but the problem now, I can't read gps location, (LocationManager) getSystemService(Context.LOCATION_SERVICE); always returning null – Omar Taha Dec 14 '17 at 14:10
  • Check the intialisation – Tomin B Azhakathu Dec 14 '17 at 14:34
  • Could you help me please, I'm stuck with this problem for a week, and I have to finish it as soon as possible. when I put isolated ="false" it works find, but when I change it to true, it returns null – Omar Taha Dec 14 '17 at 14:37
  • Check this https://stackoverflow.com/questions/24320989/locationmanager-locationmanager-this-getsystemservicecontext-location-servi – Tomin B Azhakathu Dec 14 '17 at 14:43
  • I checked it, but no luck, I read the docs about isolated service, they said: has no permissions of its own, could it be the problem? – Omar Taha Dec 14 '17 at 14:59
  • Man From this Background Service Just Call another Service with the help of this Service . That will help you to perform your task – Tomin B Azhakathu Dec 14 '17 at 15:00