Kill another application and clear it's data

前端 未结 2 971
南旧
南旧 2021-01-05 20:36

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.

相关标签:
2条回答
  • 2021-01-05 20:55

    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.

    0 讨论(0)
  • 2021-01-05 21:04

    This seems highly impossible, in my opinion. Android does not allow you to kill (or clear data) of another application.

    This link specifies a method killBackgroundProcess( String ), however the kernel would not kill it unless it is your own package name.

    0 讨论(0)
提交回复
热议问题