Is it possible to kill a “android:persistent=true” system-level app?

后端 未结 2 1616
长发绾君心
长发绾君心 2021-02-09 03:11

I have an app that is just a service that launches on boot. I am developing at the system level, so I am using android:persistent=true in order to ensure my service

相关标签:
2条回答
  • 2021-02-09 03:29
    android:persistent=true
    

    Works for preloaded applications, doesn't work for downloaded applications (e.g. APPs from Play Store, installed from apk or AS) Suggest to use foreground service method - The highest priority process. Official example here, Another full-code example here

    0 讨论(0)
  • 2021-02-09 03:39

    Figured it out. Most methods of stopping the service will not stop the persistent flag from restarting it. This includes stopService, killBackgroundService, and even System.exit(0);

    There is a hidden function called forceStopPackage in ActivityManagerService that you can invoke through the ActivityManager interface by reflection. Requires system privileges, but that shouldn't be an issue if you are using persistent.

    In Manifest:

    <uses-permission android:name="android.permission.FORCE_STOP_PACKAGES" tools:ignore="ProtectedPermissions"></uses-permission>
    

    This will kill the process. Give it name of process/package.

    try {
            Class c;
            c = Class.forName("android.app.ActivityManager");
            Method m = c.getMethod("forceStopPackage", new Class[]{String.class});
            Object o = m.invoke(null, new Object[]{"com.your.process"});
        }catch (Exception e){
                Log.e(TAG, "Failed to kill service", e);
    }
    
    0 讨论(0)
提交回复
热议问题