码迷,mamicode.com
首页 > 移动开发 > 详细

android 备份问题--转载

时间:2015-10-14 23:49:33      阅读:478      评论:0      收藏:0      [点我收藏+]

标签:

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

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

public static List<Device> findDevices(){
List<Device>devices = new ArrayList<Device>();
String str = null;
int port = 15000;
Process process = null;
Device device = null;
String[] deviceStr = new String[2];
List<String>lines=new ArrayList<String>();
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<lines.size()-1;i++){
str = lines.get(i);
deviceStr = str.split(" ");
if ("device".equals(deviceStr[1])) {
device = new Device();
device.setDeviceId(deviceStr[0]);
device.setState(deviceStr[1]);
// 目前先指定port;
device.setPort(port);
port++;
devices.add(device);
}
}
} catch (IOException e) {
e.printStackTrace();
}

return devices;
}


二、端口映射;启动手机端的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架构,你可以把服务端写成一个安卓的服务,然后可以自动处理各种客户端的命令。

android 备份问题--转载

标签:

原文地址:http://www.cnblogs.com/tuotuteng/p/4881062.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!