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.
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
– Luiz Lanza
@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.
– Luccas Melo