0

Is it possible to restart the background service after user clean the app through any third party app (CC cleaner or Kill app) in Android For API level below 24?

I will try the below code but onDestroy function not called after clean the app through this kill app(https://play.google.com/store/apps/details?id=com.tafayor.killall&hl=en)

StickyService.java

public class StickyService extends Service
{
    private static final String TAG = "StickyService";


    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e(TAG, "onStartCommand");
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        sendBroadcast(new Intent("YouWillNeverKillMe"));
    }

}

RestartServiceReceiver.java

public class RestartServiceReceiver extends BroadcastReceiver
{

    private static final String TAG = "RestartServiceReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e(TAG, "onReceive");
    context.startService(new Intent(context.getApplicationContext(), StickyService.class));

    }

}

Declare the components in manifest file:

<service android:name=".StickyService" >
</service>

<receiver android:name=".RestartServiceReceiver" >
    <intent-filter>
        <action android:name="YouWillNeverKillMe" >
        </action>
    </intent-filter>
</receiver>

Start the StickyService in a Component (i.e. Application, Activity, Fragment):

startService(new Intent(this, StickyService.class));

OR

sendBroadcast(new Intent("YouWillNeverKillMe"));

Please help me to resolve this.

Community
  • 1
  • 1

1 Answers1

1

Android OS Oreo and higher does not allow Services working in a background without the main application. Take a look at this question Background service is not working in Oreo.

Also, you may find JobScheduler useful to achieve your aim.

Oleg Sokolov
  • 1,134
  • 1
  • 12
  • 19