如何检测用户调试状态?含安卓USB调试、开发者模式及Appium自动化检测
安卓调试模式、开发者模式及自动化运行检测方案
一、检测设备的调试/开发者模式状态
1. 检测开发者模式是否开启
开发者模式的开关状态可通过系统设置项读取,适配不同安卓版本的代码示例:
import android.provider.Settings; // 读取开发者模式状态 int devModeStatus = Settings.Secure.getInt(getContentResolver(), Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0); boolean isDevModeEnabled = devModeStatus == 1;
注:部分旧版本安卓需使用Settings.Secure.DEVELOPMENT_SETTINGS_ENABLED替代Settings.Global下的常量。
2. 检测USB调试是否开启
同样通过系统设置读取USB调试的开关状态:
int usbDebugStatus = Settings.Secure.getInt(getContentResolver(), Settings.Global.ADB_ENABLED, 0); boolean isUsbDebugEnabled = usbDebugStatus == 1;
二、检测应用是否处于自动化工具(如Appium)运行状态
以下是几种可靠的检测方式,可组合使用提高准确性:
1. 检查调试器连接状态
利用安卓系统API直接判断是否有调试器附着到当前应用进程:
import android.os.Debug; boolean isDebuggerAttached = Debug.isDebuggerConnected();
2. 读取进程跟踪状态
通过/proc文件系统读取当前进程的TracerPid值,非0则表示进程被调试或跟踪:
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; boolean isUnderAutomation = false; try { String procPath = "/proc/" + android.os.Process.myPid() + "/status"; BufferedReader reader = new BufferedReader(new FileReader(procPath)); String line; while ((line = reader.readLine()) != null) { if (line.startsWith("TracerPid:")) { int tracerPid = Integer.parseInt(line.trim().split("\\s+")[1]); isUnderAutomation = tracerPid != 0; break; } } reader.close(); } catch (IOException e) { e.printStackTrace(); }
3. 检测自动化工具特征
Appium等工具运行时会留下特定标识,可通过系统属性或环境变量检测:
// 检查Appium相关标识 boolean isAppiumRunning = System.getProperty("appium") != null || System.getenv("APPIUM_SESSION_ID") != null;
4. 辅助服务状态排查
自动化工具常依赖辅助服务,可结合辅助服务状态做补充判断:
import android.accessibilityservice.AccessibilityServiceInfo; import android.content.Context; import android.view.accessibility.AccessibilityManager; AccessibilityManager accessibilityManager = (AccessibilityManager) getSystemService(Context.ACCESSIBILITY_SERVICE); boolean hasAutomationAccessibility = false; for (AccessibilityServiceInfo info : accessibilityManager.getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_ALL_MASK)) { // 检查服务包名是否属于常见自动化工具 if (info.getResolveInfo().serviceInfo.packageName.contains("appium") || info.getResolveInfo().serviceInfo.packageName.contains("uiautomator")) { hasAutomationAccessibility = true; break; } }
内容的提问来源于stack exchange,提问作者bounyh
相关产品推荐
相关产品推荐

