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);
                }
            }
        }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章