杀了另一个应用程序,并清除它的数据 [英] Kill another application and clear it's data

查看:157
本文介绍了杀了另一个应用程序,并清除它的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作,杀死选定的应用程序,并清除所有它的数据的工具。排序模拟的<一个href="http://www.howtogeek.com/howto/28496/how-to-force-kill-android-applications-without-a-task-manager/">this 我只有包名可用。

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.

推荐答案

我不知道它是否会工作或没有,但你可以做的就是让你有包名的应用程序的进程ID,然后调用 killProcess()方法,进程ID作为参数。

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: - 确定..忘了什么上面写的。我已经试过以下code,它似乎为我工作。

- 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);

您需要在您的应用程序中指定 android.permission.KILL_BACKGROUND_PROCESSES 许可清单和你是金色的。

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

EDIT2: - 下面这段code清除应用程序数据。但是有一个问题,即不能清除其他应用程序的数据,因为每一个Android应用程序在沙箱中,这从它的范围之内被访问屏蔽他们的数据运行。

- 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();
    }

它可能会使用进程类和执行来改变文件的权限搭配chmod 通过它的命令,但是话又说回来,你需要作为外壳程序运行该程序包需要被设置为可调试的 Android的清单文件的应用程序。

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.

这篇关于杀了另一个应用程序,并清除它的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆