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.