I know there are lots of solution for long life Android service on net,but none of them works for me on Android 9.So Any help you offer here is highly appreciated. My serveice implements SensorEventListener and I want to monitor 3 sensors change of handset continously,even when the main activity goes to background or gets killed(I'm aware of battery life,it's not the problem here).The solutions I have experienced are :
1-Using startForegroundService(as google developer suggests)
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
Intent i = new Intent(context, myService.class);
startForegroundService(i);
}else {
Intent i = new Intent(context, myService.class);
startService(i);
}
2-Call startForeground at the end of service onCreate
Notification nf=GetNotification();
startForeground(NOTIFICATION_ID, nf);
3-override onDestroy & onTaskRemoved (seperately and both together) and call sendBroadcast to send an intent to a broadcastReceiver that starts service inside its onReceive callback
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
Intent tnt=new Intent("com.myAppPackage.appName.actionName");
sendBroadcast(tnt);
}
public void onDestroy() {
super.onDestroy();
Intent tnt=new Intent("com.myAppPackage.appName.actionName");
sendBroadcast(tnt);
}
and broadCastReceiver :
private class clsKeepServiceAlive extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
Intent i = new Intent(context, myService.class);
startForegroundService(i);
}else {
Intent i = new Intent(context, myService.class);
startService(i);
}
}
}
private clsKeepServiceAlive m_keepServiceAlive=new clsKeepServiceAlive();
and register this receiver dynamically and explicity in service OnCreate as android developer documentation recommends:
IntentFilter iflt4=new IntentFilter();
iflt4.addAction("com.myAppPackage.appName.actionName");
iflt4.addAction("android.intent.action.LOCKED_BOOT_COMPLETED");
iflt4.addAction("android.intent.action.BOOT_COMPLETED");
registerReceiver(m_keepServiceAlive, iflt4);
and the last solution I tried without any success
5- use service attributes in Manifest :
<service
android:name=".myService"
android:enabled="true"
android:exported="false"
android:process=":myservicename"
android:directBootAware="true"
android:label="@string/app_name"
android:stopWithTask="false" />
But none of these all solutions works for me.I have put a message in the onDestroy call back of service so it writes in database when the service gets destroyed(to get sure about when service destroyed) and when my app goes to background or killed by user I definitely receive notification(foreground service notification) and I see it on the app icon (a red 1 over icon) but after some minutes (for example 20 minutes)when I back to handset I see the icon is without notification sign,the notification itself disapeared and service has been destroyed(I can see its message next time I run app). Afetr almost 1 week trying any solution suggested on the net,I have to add another question about this matter and ask you guys for a certain help. here is part of app gradle :
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.myAppPackage.appName"
minSdkVersion 23
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}