java 封裝一個ADB

1.首先項目結構

項目結構圖

2.其次,核心代碼

/**
* 使用java.lang.Runtime.exec(); 來調用ADB  其中 value是 指令內容
*/
public static Process exec(String value) throws IOException {
	String path = System.getProperty("user.dir");
	return java.lang.Runtime.getRuntime().exec(path+"/adb/adb.exe " + value);
}

2.1下載ADB程序到PC,並放入項目目錄下 的adb文件夾內

2.2創建指向adb目標的指令類方便開發

2.3創建一個臨時文件夾tem,用於保存從android中pull出的文件

2.3.1 工具類代碼

public class Runtime {
	public static Process exec(String value) throws IOException {
		String path = System.getProperty("user.dir");
		return java.lang.Runtime.getRuntime().exec(path+"/adb/adb.exe " + value);
	}
}

2.3.2 導入文件代碼

	/**
	 *  將數據從設備複製到PC中
	 * @param device
	 * @param in
	 * @param out
	 */
	public static void pull(String device) {
		try {
			Runtime.exec("-s "+device+" pull /usrapp/num.txt " +System.getProperty("user.dir")+"/tem/num.txt");
			Runtime.exec("-s "+device+" pull /usrapp/ipconfig " +System.getProperty("user.dir")+"/tem/ipconfig.properties");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

2.3.3 導入文件代碼

	/**
	 * 將數據從PC端複製到設備中
	 */
	public static void push(String device) {
		try {
			Runtime.exec("-s "+device+" push " +System.getProperty("user.dir")+"/tem/ipconfig.properties "+"/usrapp/ipconfig");
			Runtime.exec("-s "+device+" push " +System.getProperty("user.dir")+"/tem/num.txt "+"/usrapp/");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章