Activity中启动后台service,后台服务进行网络连接
MainActivity.java
Intent serviceIntent = new Intent(MainActivity.this, SocketService.class);
serviceIntent.putExtra("gateWay", gatewayAddr);
serviceIntent.putExtra("deviceIp", deviceAddr);
startService(serviceIntent);
startVideoPlayer();
service:
package com.mucephi.vplayer;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
public class SocketService extends Service {
String gateWay;
String deviceIp;
boolean running = false;
Socket clientSocket;
boolean isConnected = false;
public SocketService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
gateWay = intent.getStringExtra("gateWay");
deviceIp= intent.getStringExtra("deviceIp");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
super.onCreate();
//running=true;
new Thread() {
public void run() {
while (!isConnected) {
try {
clientSocket = new Socket();
clientSocket.connect(new InetSocketAddress(gateWay, 1234), 3000);
// 连接成功
isConnected = true;
Log.e("CC", "开始连接AP");
// 发送消息到服务器
boolean flag = clientSocket.isConnected();
Log.e("CC", ";连接状态为" + flag);
if (flag) {
Log.e("CC", "已经TCP连接成功,可以用遥控器了");
} else {
Log.e("CC", "TCP连接没有成功,可以用遥控器了");
}
OutputStream outputStream = clientSocket.getOutputStream();
PrintWriter out = new PrintWriter(outputStream);
String arg = "keyip="+deviceIp+":1234";
out.println(coder(arg));
out.flush();
// 关闭连接
// socket.close();
} catch (IOException e) {
e.printStackTrace();
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
}.start();
}
public String coder(String message){
// 依次替换 AA、CC、EE
String str = message.replaceAll("EE", "EEOE")
.replaceAll("AA", "EEOA")
.replaceAll("CC", "EEOC");
str="AA"+str+"CC";
return str;
}
@Override
public void onDestroy() {
super.onDestroy();
//running = false;
}
}
在方法中开启一个线程:
public void connectToServer(final String serverIp, final int serverPort,final String deviceIp) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
Log.e("CC", "开始执行socket线程");
try {
Socket clientSocket = new Socket(serverIp, serverPort);
Log.e("CC", "开始连接AP");
// 发送消息到服务器
boolean flag = clientSocket.isConnected();
Log.e("CC", ";连接状态为" + flag);
if (flag) {
Log.e("CC", "已经TCP连接成功,可以用遥控器了");
} else {
Log.e("CC", "TCP连接没有成功,可以用遥控器了");
}
OutputStream outputStream = clientSocket.getOutputStream();
PrintWriter out = new PrintWriter(outputStream);
String arg = "keyip=" + deviceIp + ":1234";
out.println(arg);
//out.println(coder(arg));
out.flush();
// 关闭连接
// socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
thread.start();
}
其实这样更加的方便,毕竟这个线程就只用一次,用不着再创建一个继承Thread类的自定义类了,反正又不是需要用很多次。
原文地址:https://blog.csdn.net/qq_63053996/article/details/136472167
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!