这个还正在完善中,菜鸟初试,忘大神指点。
把已经写了的一些记录以下
创建以下文件:
AdbRequest这个文件主要是用作发送adb请求的,app这个文件主要是android手机操作的一些类,device主要是一个设备对象,下面的devices里面有获取设备的方法。
家下来可能要把device和devices合并好一些,现在这样看着总别扭
package bony.adb;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
public class AdbRequest {
/**
* adb命令发送
* @author bony
* @param param 发送的命令
* @param isResult 是否有返回值
* @return results 返回值
*/
@SuppressWarnings("unused")
public ArrayList<String> Request(String param,boolean isResult){
// System.out.println("AdbRequest...");
StringBuilder outresult = new StringBuilder();
OutputStream oupt= null;
BufferedWriter outputStream=null;
BufferedReader inputStream = null,errorReader=null;
String result ;
ArrayList<String> results=new ArrayList<String>();
try {
Process process= Runtime.getRuntime().exec(param);
oupt=process.getOutputStream();
outputStream = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
inputStream = new BufferedReader(new InputStreamReader(process.getInputStream()));
errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
while((result=inputStream.readLine()) != null&&isResult) {
results.add(result.trim());
}
while ((result = errorReader.readLine()) != null&&isResult) {
results.add(result.trim());
}
process.waitFor();
inputStream.close();
errorReader.close();
}catch(Exception e){
System.err.println("AdbRequest"+e.getMessage());
}finally{
if(inputStream!=null)
try {
inputStream.close();
if(errorReader!=null) errorReader.close();
outputStream.close();
} catch (IOException e) {
System.err.println("AdbRequest"+e.getMessage());
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return results;
}
}
package bony.adb;
import java.io.File;
public class App {
String AppPackage=null;
String AppActivity=null;
String AppPath=null;
/**
* 设置App的包名
* @param AppPackageName
*/
AdbRequest request=new AdbRequest();
public void setAppPackage(String AppPackageName){
AppPackage=AppPackageName;
}
/**
* 设置App的主Activity名称
* @param AppActivityName
*/
public void setAppActivity(String AppActivityName){
AppActivity=AppActivityName;
}
/**
* 设置App路径
* @param AppPathName
*/
public void setAppPath(String AppPathName){
AppPath=new File(AppPathName).getAbsolutePath();
}
/**
* 安装App
*/
public void instaApp(){
System.out.println(request.Request("adb install -r "+AppPath, true));
}
/**
* 安装App
*/
public void instaApp(Device device){
request.Request("adb -s "+device.getDeviceId()+" install -r "+AppPath, true);
}
/**
* 启动App
*/
public void runApp(){
request.Request("adb shell am start -n "+AppPackage+"/"+AppActivity, false);
}
/**
* 判断是否当前App
* @return
*/
public boolean isApp(){
boolean flag=true;
request.Request("adb shell am start -n "+AppPackage+"/"+AppActivity, true);
return flag;
}
}
package bony.adb;
public class Device {
String DEVICENAME=null;
String DEVICEID=null;
/**
* 设置设备名称
* @param DeviceName
*/
public void setDeviceName(String DeviceName){
DEVICENAME=DeviceName;
}
/**
* 获取设备名称
* @return
*/
public String getDeviceName(){
return DEVICENAME;
}
/**
* 设置设备唯一标示码
* @param DeviceId
*/
public void setDeviceId(String DeviceId){
DEVICEID=DeviceId;
}
/**
* 获取设备唯一标示码
* @return
*/
public String getDeviceId(){
return DEVICEID;
}
}
package bony.adb;
import java.util.ArrayList;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class Devices {
// String Device
/**
* 获取设备列表
* @return
*/
public ArrayList<Device> getDevice(){
ArrayList<Device> devices=new ArrayList<Device>();
JSONObject DEVJson=new Devices().getDevicesJSONObject();
if(DEVJson.get("ret").equals("0")){
JSONArray DEV=DEVJson.getJSONArray("device");
for(int i=0;i<DEV.size();i++){
Device device=new Device();
device.setDeviceName(DEV.getJSONObject(i).getString("Name"));
device.setDeviceId(DEV.getJSONObject(i).getString("Id"));
devices.add(device);
}
// RunTest();
}
else{
System.err.println(DEVJson.get("message"));
}
return devices;
}
/**
*获取json格式设备信息
* @author 80071482
* @return devices
*/
public JSONObject getDevicesJSONObject(){
System.out.println("getDevice...");
JSONObject Dwevices=new JSONObject();
ArrayList<String> Request=new AdbRequest().Request("adb devices -l",true);
if(Request.size()>2){
Dwevices.put("ret","0");
JSONObject phone=new JSONObject();
JSONArray phones=new JSONArray();
for(int i=1;i<Request.size()-1;i++){
String linestr=Request.get(i);
String name=linestr.substring(linestr.indexOf("device:")+7,linestr.length()).trim();
String Id=linestr.substring(0,linestr.indexOf("device")).trim();
phone.put("Name",name);
phone.put("Id",Id);
phones.add(phone);
// devices.add(Request.get(i).substring(0,23).trim());
}
Dwevices.put("device",phones);
}
else{
Dwevices.put("ret","1");
Dwevices.put("message","List of devices attached");
}
// System.out.println(new JsonTools().format(Dwevices.toString()));//打印设备信息
return Dwevices;
}
}