Java通过ADB与Android客户端通讯进行数据应用备份

Home / Android MrLee 2015-4-12 2930

要实现类似碗豆夹这类的与pc应用进行通讯备份通讯录,短信,应用,并对这些信息进行编辑的功能。 这里主要用到了 adb,socket,pc 应用通过 usb 联接 android 然后通过发送广播来启动 android 里的服务器端程序。 这是采用 c/s 的通讯模式,android 端应用是服务器,pc端的是客户端。pc应用启动android端后通过 tcp 协议进行 通讯。

一、获取与电脑连接的所有手机,在前面VC++文章也有提到获取方法。这里只介绍JAVA的。其实都一样。用的是ADB命令。。。

public static List findDevices(){
		Listdevices = new ArrayList();
		String str = null;
		int port = 15000;
		Process process = null;
		Device device = null;  
		String[] deviceStr = new String[2];
		Listlines=new ArrayList();
		try {
			process = Runtime.getRuntime().exec("adb devices");
			InputStream in = process.getInputStream();
			BufferedReader read=new BufferedReader(new InputStreamReader(in));
			
			 while ((str=read.readLine())!=null){
				 lines.add(str);  
				 System.out.println(str);
			 }
			 for(int i=1;i

二、端口映射;启动手机端的app

public static void portForwardBydevice(Devices device){
		String a = null;
		String b = null;
		String c = null;
		String d = null;
		Log logger = LogFactory.getLog(DeviceFinder.class);
		
			a = "adb -s " + device.getDeviceId()+ " shell am broadcast -a NotifyServiceStop";
			b = "adb -s " + device.getDeviceId() + " forward tcp:"+device.getPort()+" tcp:12222";
			c = "adb -s " + device.getDeviceId()+ " shell am broadcast -a NotifyServiceStart";
			d= "adb -s " + device.getDeviceId()+ " shell am start -n com.newland.realmobiledetection/com.newland.realmobiledetection.system.activity.WelcomeActivity";
			logger.error("......device...a.."+a);
			logger.error("......device...b.."+b);
			logger.error("......device...c.."+c);
			logger.error("......device...d.."+d);
			try {
				Runtime.getRuntime().exec(d);				
				Thread.sleep(1000);	
				Runtime.getRuntime().exec(a);				
				Thread.sleep(1000);				
				Runtime.getRuntime().exec(b);
				Thread.sleep(1000);
				Runtime.getRuntime().exec(c);
				Thread.sleep(1000);
				logger.error("端口映射完成。。");
			} catch (IOException e) {
				
				logger.error("与手机通信异常"+e.getMessage());				
			}catch (InterruptedException e) {
				logger.error("线程中断异常"+e.getMessage());
			}
			
		}
	

三、连接:

try {  
	        InetAddress serverAddr = null;  
	        serverAddr = InetAddress.getByName("127.0.0.1"); 
	        socket = new Socket(serverAddr, pcPort);  
	        socket.setKeepAlive(true); 
	        socket.setSoTimeout(120 * 1000);
	        BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());  
	        BufferedInputStream in = new BufferedInputStream(socket.getInputStream());    
			out.write(requestContent.getBytes());  
            out.flush();  
			responseContent=readFromSocket(in);
			System.out.println(":::::::::::::receive::::::::::::" + responseContent);
			logger.info(":::::::::::::receive::::::::::::" +responseContent);
		 	obj = gson.fromJson(responseContent, method.getGenericReturnType());

手机端app);

serverSocket =new ServerScoket(12222);
Socket socket = serverSocket.accept();
out = new BufferedOutputStream(socket.getOutputStream());
  in = new BufferedInputStream(socket.getInputStream());
实现方式就是C/S架构,你可以把服务端写成一个安卓的服务,然后可以自动处理各种客户端的命令。

本文链接:https://www.it72.com/2171.htm

推荐阅读
最新回复 (0)
返回