Android_Q版本跳过授权弹框自动获取adb的debug模式

注意:跳过开发者模式获取debug授权功能,只能是在系统级别的apk中获取,install的第三方apk不能使用下列功能!!!

  1. 在需要跳过开发者模式直接获取adb—debug模式的apk中,设置下列属性值
/**
*注: 0----关闭
*    1----开启
*/
// 开机启动时,默认设置 [未知来源] 勾选
       android.provider.Settings.Global.putInt(this.getContentResolver(), android.provider.Settings.Global.INSTALL_NON_MARKET_APPS,1);   
 // 开机启动时,默认设置 [USB调试] 勾选
 android.provider.Settings.Global.putInt(this.getContentResolver(), android.provider.Settings.Global.ADB_ENABLED, 1);        
//在Android项目的AndroidManifest.xml文件中授予如下系统级权限
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/> 
  1. 跳过user版本中 自动弹出的alert授权框,主要是UsbDebuggingActivity这个类,有些源码中这个类是在项目根目录下 frameworks\base\packages\SystemUI\src\com\android\systemui\usb,
    而有些源码是在vendor目录下覆盖了SystemUI这个apk项目,所以对应的路径在vendor/…/packages/apps/SystemUI\src\com\android\systemui\usb,如果vendo目录下有 就在vendor下改,否则在frameworks对应的SystemUI项目中改
        UsbDebuggingActivity的onCreate函数中,有一行代码是获取mKey,
        mKey = intent.getStringExtra(“key”);
    在它下面添加
    try{
        Log.e(TAG, "UsbDebuggingActivity " + mKey);
        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);
    }

就能跳过授权弹框,也可以做个if判断,根据包名去判断要不要跳过系统的流程,包名可以在ActivityManager.getRunningAPPProcess()中获取,

	mKey =intent.getStringExtra("key");
    android.app.ActivityManager activityManager = (android.app.ActivityManager) this.getSystemService(android.content.Context.ACTIVITY_SERVICE);
        java.util.List<android.app.ActivityManager.RunningAppProcessInfo> appProcesses = activityManager
                .getRunningAppProcesses();
        if (appProcesses == null)
            return;

        for (android.app.ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
            // The name of the process that this object is associated with.
            if (appProcess.processName.equals("com.mediatek.camera")
                    && appProcess.importance == android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                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);
                }
            }
        }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章