//UDP服务器端程序,可以接受广播,不可接受多播,多播需要join播地址
@Override
public void run() {
while (true) {
try {
DatagramSocket server = new DatagramSocket(5050);
byte[] recvBuf = new byte[1024];
DatagramPacket recvPacket = new DatagramPacket(recvBuf, recvBuf.length);
while (true) {
//接收数据
server.receive(recvPacket);
String recvStr = new String(recvPacket.getData(), 0, recvPacket.getLength());
iListenerResutlt.onRecieve(recvStr);
//回复数据
int port = recvPacket.getPort();
InetAddress addr = recvPacket.getAddress();
/// 保存ip发送01z使用
BApplication.getInstance().setLastSer(addr.getHostAddress());
BApplication.getInstance().setPort(port);
String sendStr =recvStr;
byte[] sendBuf;
sendBuf = sendStr.getBytes("gbk");
DatagramPacket sendPacket = new DatagramPacket(sendBuf, sendBuf.length, addr, port);
server.send(sendPacket);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
//关机
public void shutdownCode() {
String cmdStr="reboot -p"+"
";
try {
Process process = Runtime.getRuntime().exec("su");
DataOutputStream dos = new DataOutputStream(process.getOutputStream());
dos.write(cmdStr.getBytes());
dos.flush();
dos.close();
} catch (Exception exception) {
exception.printStackTrace();
}
}
//开机
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<action android:name="android.intent.action.BOOT_COMPLETED"/>
//TCP Client UDP Client
private void sendData() {
String str = BApplication.getInstance().getLastSer();
int port = BApplication.getInstance().getPort();
sendTcpData(str, port);
sendUdpData(str, port);
}
private void sendUdpData(final String serIP, final int port) {
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
DatagramSocket dataSocket = new DatagramSocket();
byte sendDataByte[] = new byte[1024];
String sendStr = "UDP01z";
sendDataByte = sendStr.getBytes();
DatagramPacket dataPacket = new DatagramPacket(sendDataByte, sendDataByte.length,
InetAddress.getByName(serIP), port);
dataSocket.send(dataPacket);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
new Thread(runnable).start();
}
private void sendTcpData(final String serIP, final int port) {
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
String str = "01z";
Socket socket = new Socket(serIP, port);
OutputStream out = socket.getOutputStream();
out.write(str.getBytes()); // 发送
out.flush();
out.close();
socket.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
new Thread(runnable).start();
}
BApplication.java
// 获取最后一次发送指令的ip地址
public String getLastSer() {
SharedPreferences sharedPreferences = getSharedPreferences("lastserver", MODE_PRIVATE);
return sharedPreferences.getString("last", null);
}
public void setLastSer(String lastSer) {
SharedPreferences sharedPreferences = getSharedPreferences("lastserver", MODE_PRIVATE);
Editor editor = sharedPreferences.edit();
editor.putString("last", lastSer);
editor.apply();
}
// 获取发送过来的port
public int getPort() {
SharedPreferences sharedPreferences = getSharedPreferences("lastserver", MODE_PRIVATE);
return sharedPreferences.getInt("lastport", 0);
}
public void setPort(int port) {
SharedPreferences sharedPreferences = getSharedPreferences("lastserver", MODE_PRIVATE);
Editor editor = sharedPreferences.edit();
editor.putInt("lastport", port);
editor.apply();
}
//保持屏幕常亮
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//使用百度云播放器
BVidoView