【Android】java中如何判断设备是否在root状态

前言

客户需求,需要判断设备是否执在root状态。可以理解为是否执行了adb root 设置root状态,已经adb unroot设置unroot状态。

代码分析

分析adb deamon发现:在执行adb root 、adb unroot指令时,系统会更新service.adb.root 变量。

bash 复制代码
void restart_root_service(unique_fd fd) {
    if (getuid() == 0) {
        WriteFdExactly(fd.get(), "adbd is already running as root\n");
        return;
    }
    if (!__android_log_is_debuggable()) {
        WriteFdExactly(fd.get(), "adbd cannot run as root in production builds\n");
        return;
    }

    LOG(INFO) << "adbd restarting as root";
    android::base::SetProperty("service.adb.root", "1");
    WriteFdExactly(fd.get(), "restarting adbd as root\n");
}

void restart_unroot_service(unique_fd fd) {
    if (getuid() != 0) {
        WriteFdExactly(fd.get(), "adbd not running as root\n");
        return;
    }

    LOG(INFO) << "adbd restarting as nonroot";
    android::base::SetProperty("service.adb.root", "0");
    WriteFdExactly(fd.get(), "restarting adbd as non root\n");
}

实现

系统应用可以通过SystemProperties get "service.adb.root" value来判断是否root。

bash 复制代码
public static final String ADB_ROOT_PROP = "service.adb.root";

public static boolean deviceIsRooted(){
return SystemProperties.getInt(ADB_ROOT_PROP,0) == 1 ? true:false;
}
相关推荐
嘿嘿潶黑黑3 分钟前
Qt中的Q_PROPERTY宏
开发语言·qt
一个帅气昵称啊4 分钟前
C# 14 中的新增功能
开发语言·c#
阿蒙Amon5 分钟前
C#每日面试题-简述C#构造函数和析构函数
java·开发语言·c#
kaikaile19956 分钟前
同伦算法求解非线性方程组的MATLAB实现与优化
开发语言·算法·matlab
weixin_445054728 分钟前
力扣热题53
开发语言·python
Rysxt_9 分钟前
Go语言:现代编程的效率与并发之选
开发语言·后端·golang
musenh11 分钟前
spring学习1
java·学习·spring
txinyu的博客20 分钟前
C++ 模板元编程 (TMP)
开发语言·c++
数据大魔方20 分钟前
【期货量化实战】豆粕期货量化交易策略(Python完整代码)
开发语言·数据库·python·算法·github·程序员创富
dragoooon3426 分钟前
C++ 从零实现Json-Rpc 框架
开发语言·c++·rpc