Android不授予转储权限

4
为了监测电池使用情况,我写了一些代码来执行几个dumpsys调用,读取和解析输出以提取我感兴趣的数据。
dumpsys battery、dumpsys statusbar和dumpsys power都给我一个错误信息,如“Permission Denial: can't dump Battery service from pid...”。此外,当应用程序启动时,日志中有一个标记为“PackageManager”的项目声明:“Not granting permission android.permissionDUMP to package.... (protectionLevel = 3 ...)”。
然而,dumpsys cpuinfo和dumpsys netstat可以工作并给我正确的输出,这似乎是不一致的。
我能够从adb shell生成dumpsys battery等等命令,但是当我试图通过编程方式调用它时,它不起作用。
我尝试在HTC Nexus One手机以及模拟器上运行此代码,并获得了相同的结果。奇怪的是,这段代码在我升级到2.3之前的Nexus One手机上正常工作,现在不行了。这是因为升级的原因吗?
我尝试运行的示例代码如下:
        String command = "dumpsys battery";
    try {
        String s = null;
        Process process = Runtime.getRuntime().exec(command);


          BufferedReader stdInput = new BufferedReader(new 
                     InputStreamReader(process.getInputStream()));

                BufferedReader stdError = new BufferedReader(new 
                     InputStreamReader(process.getErrorStream()));

                // read the output from the command
                System.out.println("Here is the standard output of the command:\n");
                while ((s = stdInput.readLine()) != null) {
                    System.out.println(s);
                }

                // read any errors from the attempted command
                System.out.println("Here is the standard error of the command (if any):\n");
                while ((s = stdError.readLine()) != null) {
                    System.out.println(s);
                }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

我该如何在程序中获取正确的dumpsys输出,以及如何获取授予dump权限
*我的Nexus One手机没有Root权限,我希望在不Root的情况下让它工作,以便进行我的项目。
谢谢您的帮助。
3个回答

8

常规应用程序无法获得DUMP权限,该权限被保留给系统应用程序。


1

android.permission.Dump受到系统、签名和开发权限保护级别的保护。 源代码的第1993行显示了这一点。如果您的APK使用框架证书进行签名,位于priv-app目录中或可调试(见下文),则可以使用pm服务授予权限,但否则,代码会明确阻止您所要求的操作(源代码的第2624行)。

可以通过在build.gradle上设置debuggable属性来创建可调试的APK。示例Android DSL:

android {
    ...
    buildTypes {
        debug {
            debuggable true
            ...
        }
        quality_assurance {
            debuggable true
        }
    ...
}

0
如果您的手机已经被root,'dumpsys activity'将在Android2.3上运行:
private static void dumpIT0(String sCmd) {
    try {
        String s = null;
        String command = "su -c " + sCmd;
        Process process = Runtime.getRuntime().exec(command);

        BufferedReader stdInput = new BufferedReader(new InputStreamReader(
                process.getInputStream()));

        BufferedReader stdError = new BufferedReader(new InputStreamReader(
                process.getErrorStream()));

        // read the output from the command
        System.out.println("Here is the standard output of the command:\n");
        while ((s = stdInput.readLine()) != null) {
            System.out.println(s);
        }

        // read any errors from the attempted command
        System.out.println("Here is the standard error of the command (if any):\n");
        while ((s = stdError.readLine()) != null) {
            System.out.println(s);
        }
    } catch (IOException e) {
        Log.e(TAG, "IOException: " + e);
    }
}
sCmd = "dumpsys activity";

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接