1

I have a Android Service. Code:

public class MyService extends Service {

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

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


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

    return START_STICKY;
}

and in MainActivity - into OnCreate() i use the following code:

startService(new Intent(this, BluetoothLeService.class));

The service work fine, but when i open more apps, android kill it. I have tried with

  • START_STICKY but it doesn't work when android have a low memory available. There is a solution for this? Mmh.. I have registered the Service in manifest:

        <service android:name=".MyService"
        android:enabled="true"/>
    

...and i have tried to add android:process in manifest, but have not effect. I want keep my service in running. For me is most important.

When i open my app, i start the service and kill (only app) but service work correctly. The problem is Android OS kill my service.

(I use my app only for start MyService)

Sorry for my english. Please, someone help me? Thank you!

0x00
  • 129
  • 1
  • 13

1 Answers1

0

This is a trick which can help your service survive

@Override
    public void onDestroy() {

        startService(new Intent(MyService.this, MyService.class));

        super.onDestroy();
}
Long Uni
  • 101
  • 3
  • 12
  • 2
    This won't work. When Android kills an app process, it doesn't give any component an chance to run onDestroy. It just kills the process. http://stackoverflow.com/questions/7236782/is-an-android-service-guaranteed-to-call-ondestroy – Doug Stevenson Feb 21 '16 at 03:35
  • I have read a comment here: http://stackoverflow.com/questions/21550204/how-to-automatically-restart-a-service-even-if-user-force-close-it A SOLUTION that would work even if onDestroy isn't called everytime: send a broadcast from your service every one second and create a receiver that receives those broadcasts. Get the ellapsed time since the last broadcast message. If it is (a lot) greater than 1 second then your service isn't alive anymore -> restart it. Hope this helps Is possible implement this? can you show a example please? thank you – 0x00 Feb 21 '16 at 03:57