0

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"
}
R1349
  • 119
  • 5
  • 1) How are you testing (device make/model and OS version). 2) Are you aware of: https://dontkillmyapp.com/ – Morrison Chang Apr 28 '21 at 02:11
  • No,I did not aware of it , thank you for help.My device is Xiaomi Redmi Note 7.MIUI version 11.0.8.0 Android version 9 PKQ1.180904.001. I set the app setup according to MIUI 11 of this https://dontkillmyapp.com/ page and service is alive after about 40 minutes,I try to test app it other ways too.is there any way to set these setting programmatically so users get not pushed to do it by themselves? – R1349 Apr 28 '21 at 05:49

0 Answers0