What do you do to make sure the service is not terminated by Android?

2

I have a service that runs in the background to pick up the location and go recording in a text. The service starts along with android through BroadcastReceiver. It works perfectly, but after about 12 hours of service running, the android ends and I have to restart the device for it to run again.

Class BootReceiver:

 @Override
public void onReceive(Context context, Intent intent) {
    Intent myService = new Intent(context, LocationService.class);
    context.startService(myService);
}

My service:

public class LocationService extends Service {


@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    throw new UnsupportedOperationException("Not yet implemented");
}

public void localizacao() {

    Log.i("GPS", "Iniciou");

    final Handler handler = new Handler();

    Runnable runnable = new Runnable() {

        public void run() {

            while (true) {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                handler.post(new Runnable() {
                    public void run() {
                        Log.i("GPS", "Serviço executando");

                       // PEGA LOCALIZAÇÃO E GRAVA
                    }
                });

            }
        }
    };
    Thread t = new Thread(runnable);
    t.setPriority(Thread.MIN_PRIORITY);
    t.start();
}

@Override
public void onDestroy() {
    Intent myService = new Intent(this, LocationService.class);
    startService(myService);
}

@Override
public void onTaskRemoved(Intent rootIntent) {
    Intent myService = new Intent(this, LocationService.class);
    startService(myService);
}}

Manifest:

<service
        android:name=".LocationService"
        android:enabled="true"
        android:exported="true"
        android:singleUser="true"
        android:stopWithTask="false" />

    <receiver android:name=".BootReciever">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

I would like to know how do I stop android from terminating my service.

    
asked by anonymous 04.03.2016 / 02:11

1 answer

2

Hello,

Try to do this.

Add the onStartCommand to your Service:

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

    }

Within this method set the flag to be returned:

    return (super.onStartCommand(intent, START_REDELIVER_INTENT, startId));

In this way, if the android completes the service your service will restart automatically.

  

START_REDELIVER_INTENT

     

If the system drops the service after the onStartCommand () return,   re-create the service and call onStartCommand () with the last intention   was delivered to the service. Any pending intentions are delivered,   in its turn. This is appropriate for services that are   work that must be taken up immediately, as the   download a file.

NOTE: You have to start your location submission within the onStartCommand method.

There are other types of flags to use as START_NOT_STICKY, START_STICKY, START_REDELIVER_INTENT.

See more at: link

    
04.03.2016 / 12:08