Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

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 is not killed under memory pressure.

However, I find this prevents me from killing my service under any conditions. I still want to be able to kill it myself - is there a way to do this, or is it impossible because "persistent=true" is defined in the manifest?

If this is not possible, how else can I protect my process from dying under memory pressure? Can I define a priority by hand? Perhaps oom_score or something?

like image 576
Eric S. Avatar asked Jun 29 '16 20:06

Eric S.


People also ask

What are persistent apps in Android?

Android apps that run continuously in the background are called persistent apps. This ensures that the application is always running and receives and notifies you of messages when they come in.

How do you check App is killed or not in android?

there's no way to determine when a process is killed. From How to detect if android app is force stopped or uninstalled? When a user or the system force stops your application, the entire process is simply killed. There is no callback made to inform you that this has happened.

How do you kill apps on Android emulator?

Go to DDMS and select your App process. Click STOP icon in upper-right-hand side. It will kill the process.

What is persistent process in Android?

android:persistent. Whether or not the application should remain running at all times — "true" if it should, and "false" if not. The default value is "false" . Applications should not normally set this flag; persistence mode is intended only for certain system applications. android:process.


1 Answers

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);
}
like image 67
Eric S. Avatar answered Oct 19 '22 10:10

Eric S.