当前位置:   article > 正文

android取消自动调试模式,Android用代码实现开启关闭调试模式

pixel调试模式怎么关闭

打开Android手机的USB调试对于使用豌豆夹、调试程序等来说很重要。下面说说如何用代码自动打开USB调试。先分析USB调试的相关源代码。

在 packages/apps/Settings/src/com/android/settings/DevelopmentSettings.java 找到关于 USB Debug Enable 的代码:

Settings.Secure.putInt(getContentResolver(), Settings.Secure.ADB_ENABLED,0);

此文件中,将根据用户设置将其值保存到 Settings 数据库中。别处将根据其值动态变化做出相应动作

经搜索,在 frameworks/base/services/java/com/android/server/NotificationManagerService.java 中存在利用该值判断是否在状态栏中进行通知。代码如下:

别处将根据其值动态变化做出相应动作如状态栏消息提示。

voidobserve() {

ContentResolver resolver = mContext.getContentResolver();

resolver.registerContentObserver(Settings.Secure.getUriFor(

Settings.Secure.ADB_ENABLED), false,this);

update();

}

@OverridepublicvoidonChange(booleanselfChange) {

update();

}

publicvoidupdate() {

ContentResolver resolver = mContext.getContentResolver();

mAdbEnabled = Settings.Secure.getInt(resolver,

Settings.Secure.ADB_ENABLED, 0) !=0;

updateAdbNotification();

}

通过分析代码,我们可以实现用程序自动打开usb调试了。

booleanenableAdb = (Settings.Secure.getInt(getContentResolver(), Settings.Secure.ADB_ENABLED,0) >0);

if(!enableAdb) {

Settings.Secure.putInt(getContentResolver(), Settings.Secure.ADB_ENABLED, 1);

}

马上进行运行,会出现异常,通过Logcat可以看到没有权限。android.permission.WRITE_SECURE_SETTINGS是不允许普通程序来执行,必须要有系统的签名或放到

/system/app下。

(1)、在AndroidManifest.xml加上两个权限

可以把程序push到/system/app,并对这个apk加上0644的权限,重启手机,可以发现usb调试自动打开了。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/287903?site
推荐阅读
相关标签
  

闽ICP备14008679号