为了监视电池使用情况等,我有执行一些dumpsys调用的代码,读取并解析输出以提取我感兴趣的数据. dumpsys电池,dumpsys状态栏和dumpsys电源都给我输出错误信息,如“权限拒绝:无法从pid转储电
dumpsys电池,dumpsys状态栏和dumpsys电源都给我输出错误信息,如“权限拒绝:无法从pid转储电池服务……”
此外,当应用程序启动时,日志中有一个标记为“PackageManager”的项目,表明没有授予android.permissionDUMP权限包….(protectionLevel = 3 …)“
但是,dumpsys cpuinfo和dumpsys netstat工作并给我正确的输出,这似乎是不一致的.
我能够从adb shell生成dumpsys电池等,但是当我尝试以编程方式调用它时它不起作用.
我试过在HTC Nexus One手机和模拟器上运行它,并为每个手机获得相同的结果.奇怪的是,这个代码一天前在我的Nexus One上运行(在我从2.2升级到2.3之前),现在它没有.这是因为升级吗?
我尝试运行的代码示例如下:
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以编程方式为我提供正确的输出以及如何获得转储权限?
* Nexus One没有扎根,我想让这个工作无需为了我的项目而根目录
谢谢您的帮助
常规应用程序无法获得DUMP权限.它保留给系统应用程序.