Java 在用户通过Android中的任何第三方应用程序(CC cleaner或Kill应用程序)清理应用程序后,是否可以重新启动后台服务?

Java 在用户通过Android中的任何第三方应用程序(CC cleaner或Kill应用程序)清理应用程序后,是否可以重新启动后台服务?,java,android,service,alarm,android-broadcastreceiver,Java,Android,Service,Alarm,Android Broadcastreceiver,如果API级别低于24,用户可以通过Android中的任何第三方应用程序(CC cleaner或Kill应用程序)清理应用程序后重新启动后台服务吗? 我将尝试以下代码,但在通过此杀死应用程序清理应用程序后未调用onDestroy函数() StickyService.java public class StickyService extends Service { private static final String TAG = "StickyService";

如果API级别低于24,用户可以通过Android中的任何第三方应用程序(CC cleaner或Kill应用程序)清理应用程序后重新启动后台服务吗?

我将尝试以下代码,但在通过此杀死应用程序清理应用程序后未调用onDestroy函数()

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"));
    }

}
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));

    }

}
RestartServiceReceiver.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"));
    }

}
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));

    }

}
在清单文件中声明组件:

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

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

在组件(即应用程序、活动、, 片段):

startService(新意图(这个,StickyService.class))

sendBroadcast(新意图(“YouWillNeverKillMe”)


请帮我解决这个问题。

Android OS Oreo及更高版本不允许服务在没有主应用程序的后台工作。 看看这个问题

此外,你可能会发现实现你的目标是有用的