Jelly Bean (api 16) 的 READ_LOGS 权限 [英] READ_LOGS permission on Jelly Bean (api 16)

查看:23
本文介绍了Jelly Bean (api 16) 的 READ_LOGS 权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于 Android Jelly Bean 不支持 日志读取权限(根据 这个 google io 2012 视频这个也是 ) ,我想知道有 root 权限的设备(或非 root 权限的设备)可以绕过此限制并能够读取日志.

Since Android Jelly Bean doesn't support the logs reading permission (according to this google io 2012 video and this one too ) , i would like to know if it's possible for rooted devices (or non-rooted devices) to be able to bypass this restriction and be able to read the logs.

我该怎么做?我真的需要将应用程序设为系统应用程序,还是足够扎根?

How do i do that? Do i really need to make the app a system app, or is rooting enough?

推荐答案

您可以通过从您的应用程序执行 pm grant 命令来获得对 root 设备的权限.不过,您可能必须在此之后重新启动应用程序才能使更改生效:

You can obtain the permission on a rooted device by executing the pm grant command from your app. Probably you will have to restart the app after that for the change to take effect, though:

String pname = getPackageName();
String[] CMDLINE_GRANTPERMS = { "su", "-c", null };
if (getPackageManager().checkPermission(android.Manifest.permission.READ_LOGS, pname) != 0) {
    Log.d(TAG, "we do not have the READ_LOGS permission!");
    if (android.os.Build.VERSION.SDK_INT >= 16) {
        Log.d(TAG, "Working around JellyBeans 'feature'...");
        try {
            // format the commandline parameter
            CMDLINE_GRANTPERMS[2] = String.format("pm grant %s android.permission.READ_LOGS", pname);
            java.lang.Process p = Runtime.getRuntime().exec(CMDLINE_GRANTPERMS);
            int res = p.waitFor();
            Log.d(TAG, "exec returned: " + res);
            if (res != 0)
                throw new Exception("failed to become root");
        } catch (Exception e) {
            Log.d(TAG, "exec(): " + e);
            Toast.makeText(context, "Failed to obtain READ_LOGS permission", Toast.LENGTH_LONG).show();
        }
    }
} else
    Log.d(TAG, "we have the READ_LOGS permission already!");

应该从您的 onCreate() 调用此代码.授予权限后,不再需要根权限.

This code should be called from your onCreate(). Once the permission is granted, no more root powers are required.

P.S:超级用户应用上的 p.waitFor() 会阻塞,延迟您的应用启动并可能导致 ANR.

P.S: The p.waitFor() blocks on the Superuser app, delaying your app start and potentially cause an ANR.

这篇关于Jelly Bean (api 16) 的 READ_LOGS 权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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