What to do so the service is not finished by Android?

Asked

Viewed 689 times

2

I have a service that runs in the background to pick up the location and go recording in a text. The service is started together with android through Broadcastreceiver. Works perfectly, but after some 12 hours of service running, android ends and I have to restart the device to return to run.

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 wonder how do I make android not finalize my service.

1 answer

2


Hello,

Try to do it this way.

Add onStartCommand to your Service:

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

Within this method set the flag that will be returned:

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

This way, if the android end the service your service will be restarted automatically.

START_REDELIVER_INTENT

If the system deletes the service after the return of onStartCommand(), recreate the service and call onStartCommand() with the last intention that has been delivered to the service. Any pending intentions are delivered, in turn. This is suitable for services that are performing a work actively that should be resumed immediately, as the download of a file.

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

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

See more in: http://developer.android.com/intl/pt-br/guide/components/services.html

  • I tried to do this but it didn’t work, after 12h the android finished the service. I have an app that runs forever would I call the service an apk through another apk? ai would solve my problem

  • @Luizlanza by my knowledge in android, It is impossible to keep a service running all the time, when android need memory or have another priority it can kill your service. But you can treat your reboot that happens instantly when you create the service the way I passed it to you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.