当我将我的应用移至后台时,我的服务停止监听服务器,连接未断开,这就是服务器日志的内容。为什么会发生这种行为?使用 startService 启动服务。
public class LocalService extends Service {
Socket socket = null;
public String SERVER_IP = "192.168.1.10";
public boolean connected = false;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null) {
if (intent.hasCategory("connection")) {
if (connected) {
try {
if (socket != null) {
socket.close();
connected = false;
// sendBroadcast(intent1);
}
}
catch (IOException e) {
e.printStackTrace();
}
}
else {
connect();
}
}
}
return START_STICKY;
}
public void connect() {
Thread thread = new Thread() {
@Override
public void run() {
// sendBroadcast(intent1);
try {
socket = new Socket(SERVER_IP, 5000);
socket.setSoTimeout(5000);
connected = true;
// sendBroadcast(intent2);
OutputStream out = socket.getOutputStream();
PrintWriter output = new PrintWriter(out);
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while (true) {
String event = input.readLine();
// sendBroadcast(intent3);
if (event.substring(0, 3).equals("msg")) {
String notificationTitle = event.substring(4, event.indexOf("::"));
CharSequence tickerText = event.substring(event.indexOf("::") + 2);
String notificationContent = event.substring(event.indexOf("::") + 2);
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(LocalService.this)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(largeIcon)
.setTicker(tickerText)
.setContentTitle(notificationTitle)
.setContentText(notificationContent)
.setOnlyAlertOnce(true);
Intent resultIntent = new Intent(LocalService.this, ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(LocalService.this);
stackBuilder.addParentStack(ResultActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(mId, mBuilder.build());
mId = mId + 1;
}
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (java.net.ConnectException e) {
e.printStackTrace();
// sendBroadcast(intent4);
} catch (SocketTimeoutException e) {
e.printStackTrace();
} catch(java.net.SocketException e) {
e.printStackTrace();
connected = false;
// sendBroadcast(intent5);
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
connected = false;
// sendBroadcast(intenty);
}
}
};
thread.start();
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
private final IBinder mBinder = new LocalBinder();
}
最佳答案
似乎您在绑定(bind)和取消绑定(bind)您的 Activity 到您的服务时遇到了一些问题。
我猜你没有取消绑定(bind)到该服务的 Activity ,所以在你的 Activity 停止/终止后你的服务停止工作并且由于 START_STICKY
它将由系统再次启动。
您应该在 onPause()
中取消绑定(bind) Activity ,并在服务的 onDestory
方法中断开套接字。
关于android - 当应用程序发送到后台时,TCP 连接停止监听,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25433309/