2024-04-24 14:47:33 | 我爱编程网
打开Android手机的USB调试对于使用豌豆夹、调试程序等来说很重要。下面说说如何用代码自动打开USB调试。先分析USB调试的相关
源代码
。
在 packages/apps/Settings/src/com/android/settings/DevelopmentSettings.java 找到关于 USB Debug Enable 的代码:
[java] view plain copy
Settings.Secure.putInt(getContentResolver(), Settings.Secure.ADB_ENABLED, 0 );
此文件中,将根据用户设置将其值保存到 Settings 数据库中。别处将根据其值动态变化做出相应动作
经搜索,在 frameworks/base/services/java/com/android/server/NotificationManagerService.java 中存在利用该值判断是否在状态栏中进行通知。代码如下:
别处将根据其值动态变化做出相应动作如状态栏消息提示。
[java] view plain copy
void observe() {
ContentResolver resolver = mContext.getContentResolver();
resolver.registerContentObserver(Settings.Secure.getUriFor(
Settings.Secure.ADB_ENABLED), false , this );
update();
}
@Override public void onChange( boolean selfChange) {
update();
}
public void update() {
ContentResolver resolver = mContext.getContentResolver();
mAdbEnabled = Settings.Secure.getInt(resolver,
Settings.Secure.ADB_ENABLED, 0 ) != 0 ;
updateAdbNotification();
}
通过分析代码,我们可以实现用程序自动打开usb调试了。
[java] view plain copy
boolean enableAdb = (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加上两个权限
[html] view plain copy
可以把程序push到/system/app,并对这个apk加上0644的权限,重启手机,可以发现usb调试自动打开了。
要使用adb 我们首先要有android 的环境,android还需要java的环境首先讲如何配置adb,使adb可以在终端使用,也就是在命令行可以使用,配置环境变量,在计算机右键选择属性,我们会看到如下界面,选择【高级系统设置】,然后点击【环境变量】
下面是我配置的android的环境变量和java的环境变量,大家需要自行配置
配置完成后,我们在终端输入adb命令,会看到adb的相关信息我爱编程网
执行adb devices 命令,我们可以看到当前电脑正在连接调试的手机
首先我们学习下,如何拿adb安装apk到某个设备,假如只连接了一台设备,只需要adb install apk的路径 就可以直接安装成功,假如有多台设备,我们就需要用到-s选项选择设置编号,设备编号通过上面的adb devices可以得到,完整命令 adb -s 设备编号 install apk的路径,具体执行成功示例,如下图展示
卸载的话直接加adb uninstall 包名,就可以直接卸载啦
代码如下:
package com.symbio.ltp.adb;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import com.symbio.ltp.model.ConfigPropertiesData;
import com.symbio.ltp.util.Log;
public class ShellCommand {
private String name;
private Process process;
private BufferedWriter writer;
private BufferedReader reader;
private BufferedReader errorReader;
private List
private String[] returnValue;
public ShellCommand(String name) {
this.name = name;
}
public String getName() {
return name;
}
public Process getProcess() {
return process;
}
public BufferedWriter getOutputWriter() {
return writer;
}
public BufferedReader getInputReader() {
return reader;
}
public BufferedReader getErrorReader() {
return errorReader;
}
public boolean start(String cmd) {
try {
process = Runtime.getRuntime().exec(cmd);
writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
} catch (IOException e) {
Log.debug("Exception in shell(" + name + ") -- " + e.getMessage());
return false;
}
return true;
}
public boolean exec(String cmd) {
String line;
try {
writer.write(cmd + "\n");
writer.flush();
while((line = reader.readLine()) != null) {
Log.debug(line);
if(line.equals(ConfigPropertiesData.ltp_success)) {
return true;
} else if(line.equals(ConfigPropertiesData.ltp_fail)) {
return false;
}
}
} catch (IOException e) {
Log.debug("Exception in shell(" + name + ") -- " + e.getMessage());
return false;
}
return true;
}
public String [] execReturn(String cmd) {
String line;
list = new ArrayList
try {
writer.write(cmd + "\n");
writer.flush();
line = reader.readLine();
while((line = reader.readLine()) != null) {
if(line.length()>0 && !(line.startsWith("#"))){
Log.debug(line);
list.add(line);
if(line.equals(ConfigPropertiesData.ltp_success)) {
break;
} else if(line.equals(ConfigPropertiesData.ltp_fail)) {
break;
}
}
}
int size = list.size();
returnValue = new String[size];
for (int i = 0; i < size; i++) {
returnValue[i] = list.get(i);
}
} catch (IOException e) {
Log.debug("Exception in shell(" + name + ") -- " + e.getMessage());
return null;
}
return returnValue;
}
public void terminate() {
try {
writer.write(0x03);
writer.flush();
} catch (IOException e) {
Log.debug("Exception in shell(" + name + ") -- " + e.getMessage());
}
}
}
2024-01-01 09:58:48
2024-04-13 00:38:12
2024-03-31 15:14:06
2024-03-21 00:22:13
2024-02-10 22:15:59
2024-04-11 19:37:19