public class FileOptions {
public String name;
public String path;
public long size;
}
//Activity
public class MainActivity extends Activity {
private TextView tvMsg;
private EditText logShow, filePath;
private Handler handler;
private SocketManager socketManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout. activity_main);
tvMsg = (TextView)findViewById(R.id. tvMsg);
logShow = (EditText)findViewById(R.id. log_show);
handler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch(msg. what){
case 0:
SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss" );
logShow.append( "\n[" + format.format(new Date()) + "]" + msg.obj .toString());
break;
case 1:
tvMsg.setText( "请在PC端输入IP:" + GetIpAddress() + " 端口:" + msg.obj .toString());
break;
case 2:
Toast. makeText(getApplicationContext(), msg.obj.toString(), Toast. LENGTH_SHORT).show();
break;
}
}
};
socketManager = new SocketManager( handler);
}
@Override
protected void onDestroy() {
super.onDestroy();
System. exit(0);
}
public String GetIpAddress() {
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE );
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int i = wifiInfo.getIpAddress();
return (i & 0xFF) + "." +
((i >> 8 ) & 0xFF) + "." +
((i >> 16 ) & 0xFF)+ "." +
((i >> 24 ) & 0xFF );
}
}
//socket管理
public class SocketManager {
private static final String FILE_PATH= "/glass";
private static final String TAG = "SocketManager";
private ServerSocket server;
private Handler handler = null;
private List<FileOptions> fileList;
public SocketManager(Handler handler) {
this. handler = handler;
int port = 9999;
while (port > 9000) {
try {
server = new ServerSocket(port);
break;
} catch (Exception e) {
port--;
}
}
SendMessage
Android和PC端通过局域网文件同步
最新推荐文章于 2023-09-24 21:19:31 发布
该博客介绍了如何使用Android设备与PC通过局域网进行文件同步。在Android应用中,实现了利用Socket通信和JSON解析来发送文件索引,然后接收和发送文件。PC端通过接收文件索引,建立连接并接收文件。整个过程涉及了C++、Android编程、Socket通信和jsoncpp库的使用。
摘要由CSDN通过智能技术生成