There's a new API introduced in API 19 (KitKat):
ActivityManager.clearApplicationUserData().
I highly recommend using it in new applications:
import android.os.Build.*;
if (VERSION_CODES.KITKAT <= VERSION.SDK_INT) {
((ActivityManager)context.getSystemService(ACTIVITY_SERVICE))
.clearApplicationUserData(); // note: it has a return value!
} else {
// use old hacky way, which can be removed
// once minSdkVersion goes above 19 in a few years.
}
If you don't want the hacky way you can also hide the button on the UI, so that functionality is just not available on old phones.
Knowledge of this method is mandatory for anyone using android:manageSpaceActivity
.
Whenever I use this, I do so from a manageSpaceActivity
which has android:process=":manager"
. There, I manually kill any other processes of my app. This allows me to let a UI stay running and let the user decide where to go next.
private static void killProcessesAround(Activity activity) throws NameNotFoundException {
ActivityManager am = (ActivityManager)activity.getSystemService(Context.ACTIVITY_SERVICE);
String myProcessPrefix = activity.getApplicationInfo().processName;
String myProcessName = activity.getPackageManager().getActivityInfo(activity.getComponentName(), 0).processName;
for (ActivityManager.RunningAppProcessInfo proc : am.getRunningAppProcesses()) {
if (proc.processName.startsWith(myProcessPrefix) && !proc.processName.equals(myProcessName)) {
android.os.Process.killProcess(proc.pid);
}
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…