Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kill another application and clear it's data

Tags:

android

I am working on a tool that kills selected application and clears all it's data. Sort of simulating this I have only package name available.

like image 549
Heisenberg Avatar asked Nov 01 '22 11:11

Heisenberg


1 Answers

Am not sure whether it would work or not but what you can do is get the process ID of the app with the package name you have and then call killProcess() method with process ID as the parameter.

EDIT1:- ok.. forget whats written above.. I've tried the following code and it seems to work for me.

ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
activityManager.killBackgroundProcesses(appProcessName);

You need to specify android.permission.KILL_BACKGROUND_PROCESSES permission in your application manifest and you are golden.

EDIT2:- the following piece of code clears app data. But there is a problem that it cant clear other app's data because every android app runs in a Sandbox, which shields their data from being accessed from outside its scope.

public void clearApplicationData(String packageName) {
        File cache = getCacheDir();
        File appDir1 = new File(cache.getParent()).getParentFile();
        File appDir = new File(appDir1.getAbsolutePath() + "/" + packageName);
        if (appDir.exists()) {
            String[] children = appDir.list();
            for (String s : children) {
                if (!s.equals("lib")) {
                    deleteDir(new File(appDir, s));
                    Toast.makeText(this, "App Data Deleted", Toast.LENGTH_LONG)
                            .show();
                }
            }
        }
    }

public static boolean deleteDir(File dir) {
        if (dir != null && dir.isDirectory()) {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }
        return dir.delete();
    }

It may be possible to change the files' permission by using Process class and executing chmod command through it but then again you'll need to run as shell for that package which requires the application to be set as debuggable in the Android-Manifest file.

So bottom-line is its tough if not impossible to clear other app's data.

like image 156
d3m0li5h3r Avatar answered Nov 15 '22 04:11

d3m0li5h3r