Android Framework 常见解决方案(21)默认开启adb

1 原理解读

本质上要解决两个问题:

  • 设置默认ADB enable为真,android的user版本中也默认为真,即使之有效。
  • 一般android设备连接电脑PC后会出现USB连接的弹框,去掉弹框直接生效。

2 修改方案(Android Q R S)

设置默认ADB Enable为真,android的user版本中也默认为真

文件 AOSP/frameworks/base/services/core/java/com/android/server/adb/AdbService.java

在该文件的systemReady中修改

java 复制代码
    /**
     * Called in response to {@code SystemService.PHASE_ACTIVITY_MANAGER_READY} from {@code
     * SystemServer}.
     */
    public void systemReady() {
        if (DEBUG) Slog.d(TAG, "systemReady");

        // make sure the ADB_ENABLED setting value matches the current state
        try {
            //这里不管mIsAdbUsbEnabled是否为真,强制设定属性Settings.Global.ADB_ENABLED为1
            Settings.Global.putInt(mContentResolver,
-                    Settings.Global.ADB_ENABLED, mIsAdbUsbEnabled ? 1 : 0);
+                    Settings.Global.ADB_ENABLED, 1);

            Settings.Global.putInt(mContentResolver,
                    Settings.Global.ADB_WIFI_ENABLED, mIsAdbWifiEnabled ? 1 : 0);
        } catch (SecurityException e) {
            // If UserManager.DISALLOW_DEBUGGING_FEATURES is on, that this setting can't be changed.
            Slog.d(TAG, "ADB_ENABLED is restricted.");
        }
    }

修改文件AOSP/frameworks/base/packages/SystemUI/src/com/android/systemui/usb/UsbDebuggingActivity.java,一般android设备连接电脑PC后会出现USB连接的弹框,这里的修改主要是不弹框并直接连接成功,代码修改如下:

java 复制代码
public class UsbDebuggingActivity extends AlertActivity
                                  implements DialogInterface.OnClickListener {
    //...
    @Override
    public void onCreate(Bundle icicle) {
        Window window = getWindow();
        window.addSystemFlags(
                WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
        window.setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);

        super.onCreate(icicle);

        // Emulator does not support reseating the usb cable to reshow the dialog.
        boolean isEmulator = SystemProperties.get("ro.boot.qemu").equals("1");
        if (SystemProperties.getInt("service.adb.tcp.port", 0) == 0 && !isEmulator) {
            mDisconnectedReceiver = new UsbDisconnectedReceiver(this);
            IntentFilter filter = new IntentFilter(UsbManager.ACTION_USB_STATE);
            mBroadcastDispatcher.registerReceiver(mDisconnectedReceiver, filter);
        }

        Intent intent = getIntent();
        String fingerprints = intent.getStringExtra("fingerprints");
        mKey = intent.getStringExtra("key");

        if (fingerprints == null || mKey == null) {
            finish();
            return;
        }

+        try {
+            IBinder b = ServiceManager.getService(ADB_SERVICE);
+            IAdbManager service = IAdbManager.Stub.asInterface(b);
+            service.allowDebugging(true, mKey);
+            finish();
+        } catch (Exception e) {
+            Log.e(TAG, "Unable to notify Usb service", e);
+        }

        final AlertController.AlertParams ap = mAlertParams;
        ap.mTitle = getString(R.string.usb_debugging_title);
        ap.mMessage = getString(R.string.usb_debugging_message, fingerprints);
        ap.mPositiveButtonText = getString(R.string.usb_debugging_allow);
        ap.mNegativeButtonText = getString(android.R.string.cancel);
        ap.mPositiveButtonListener = this;
        ap.mNegativeButtonListener = this;

        // add "always allow" checkbox
        LayoutInflater inflater = LayoutInflater.from(ap.mContext);
        View checkbox = inflater.inflate(com.android.internal.R.layout.always_use_checkbox, null);
        mAlwaysAllow = (CheckBox)checkbox.findViewById(com.android.internal.R.id.alwaysUse);
        mAlwaysAllow.setText(getString(R.string.usb_debugging_always));
        ap.mView = checkbox;
        window.setCloseOnTouchOutside(false);
    setupAlert();
    }
    //...
}
相关推荐
字节全栈_BjO31 分钟前
mysql死锁排查_mysql 死锁问题排查
android·数据库·mysql
恋猫de小郭15 小时前
Android Studio 正式版 10 周年回顾,承载 Androider 的峥嵘十年
android·ide·android studio
aaaweiaaaaaa17 小时前
php的使用及 phpstorm环境部署
android·web安全·网络安全·php·storm
工程师老罗19 小时前
Android记事本App设计开发项目实战教程2025最新版Android Studio
android
pengyu1 天前
系统化掌握 Dart 编程之异常处理(二):从防御到艺术的进阶之路
android·flutter·dart
消失的旧时光-19431 天前
android Camera 的进化
android
基哥的奋斗历程1 天前
Openfga 授权模型搭建
android·adb
Pakho love1 天前
Linux:文件与fd(被打开的文件)
android·linux·c语言·c++
勿忘初心912 天前
Android车机DIY开发之软件篇(九) NXP AutomotiveOS编译
android·arm开发·经验分享·嵌入式硬件·mcu
lingllllove2 天前
PHP中配置 variables_order详解
android·开发语言·php