如何通过Android SDK获取root权限?

发布于 2024-10-22 01:33:20 字数 593 浏览 6 评论 0原文

我正在学习 Android 编程,我想制作一个必须以 root 身份运行的应用程序。合乎逻辑的做法是在 Android Manifest 中添加 root 权限。

我在文档中看到了此链接,并特别指出了FACTORY_TEST 权限

公共静态最终字符串FACTORY_TEST

自:API 级别 1

作为制造商测试运行 应用程序,以 root 用户身份运行。 仅当设备可用时 在制造商测试模式下运行。 常数值: “android.permission.FACTORY_TEST”

这是最好的方法吗?

如果无法使用 SDK,如何使“root”应用程序正常工作?

I'm learning Android programming, and I want to make an application which has to run as root. The logical thing would be to add a root permission in the Android Manifest.

I saw this link in the documentation, and especially noted the FACTORY_TEST permission:

public static final String FACTORY_TEST

Since: API Level 1

Run as a manufacturer test
application, running as the root user.
Only available when the device is
running in manufacturer test mode.
Constant Value:
"android.permission.FACTORY_TEST"

Is that the best way?

If it's not possible using the SDK, how can I make a "root" application work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟火散人牵绊 2024-10-29 01:33:20

您需要做的是这样的:

Process root = Runtime.getRuntime().exec("su");

这会导致超级用户显示,这让您可以允许或阻止它的根访问。如果用户未获得 root 权限,则此方法可能不起作用。 这里是一种方法你可以测试一下。

What you need to do is something like:

Process root = Runtime.getRuntime().exec("su");

That causes SuperUser to show, which lets you either Allow or Block it from root access. This approach might not work if the user is not rooted. Here is a way you can test it.

堇年纸鸢 2024-10-29 01:33:20

首先让我们打好基础。 Android底层运行Linux内核。现在,如果您必须使用超级用户权限(以 root 身份运行)在其上运行进程,唯一的方法是通过命令行执行进程,因为这是您可以直接交互的唯一方法内核。此外,您还需要在运行任何命令之前使用 su 。正如克里斯在他对第一个答案的评论中提到的那样,

Process process = Runtime.getRuntime().exec("su");

几乎什么也做不了。它只会使用对话框请求超级使用权限。您可以做的不仅仅是执行 su 您可以使用 su 执行您的进程,如下所示

Process process = Runtime.getRuntime().exec(new String[] { "su", "-c", yourCommand});

-c 选项

su 的几个选项中最常用的是 -c,它告诉 su 执行同一行中紧随其后的命令。此类命令以新用户身份执行,然后在该命令完成执行或关闭其启动的任何程序后,运行 su 的终端窗口或控制台立即返回到前用户的帐户。(< a href="http://www.linfo.org/su.html" rel="nofollow noreferrer">更多详细信息)

替代选项

上述方法的另一种替代方式可能工作是使用命令行将应用程序复制到 /system/app/ 目录。然后您的应用程序将以 root 权限自动运行(与系统应用程序相同)

First lets us get the basics right. Android run Linux kernel underneath. Now if you have to run your process on it with super user privileges(run it as root) the only way is to execute your process is via command line because it is the only way you can directly interact with the kernel. Also you need to use su before running any command. Also as Chris has mentioned in his comment on the 1st answer

Process process = Runtime.getRuntime().exec("su");

will accomplish nearly nothing. It will just ask for super use privilege using dialog. What you can do is instead of just executing su you can execute your process with su as following

Process process = Runtime.getRuntime().exec(new String[] { "su", "-c", yourCommand});

The -c Option

Among the most commonly used of su's few options is -c, which tells su to execute the command that directly follows it on the same line. Such command is executed as the new user, and then the terminal window or console from which su was run immediately returns to the account of the former user after the command has completed execution or after any program that it has launched has been closed.(More details)

Alternate Option

Alternative to above method one another way that might work is to use command line to copy you app to /system/app/ directory. Then your application will run automatically with root privileges(same as System apps)

绮烟 2024-10-29 01:33:20

SDK 不提供以 root 身份运行应用程序的方法。

The SDK does not offer a way to run an app as root.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文