软件导致连接中止:甚至从最近的列表中清除应用程序时出现套接字写入错误(它 运行 在与服务不同的线程中)
Software caused connection abort: socket write error on clearing the app from recent list even (it running in a different thread from a service)
我制作了一个应用程序,其中有“开始”按钮,当我按下它时,它会启动客户端线程服务,其中客户端套接字通过服务器套接字连接到我的电脑,它运行良好,直到应用程序被清除从最近的名单。当我按下“开始”按钮并执行 finish()
关闭应用程序时,不会发生同样的情况。服务在这两种情况下都运行,但是当我从最近的列表中清除应用程序时,服务保留 运行 但我的服务器应用程序 "Software caused connection abort: socket write error" 收到错误。当我使用 finish()
关闭应用程序时,它也会从最近的列表中清除,但服务会成功保持 运行 服务,而套接字不会出现任何错误。
我该怎么办??
对不起,我的英语不是我的母语。
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent serviceIntent = new Intent(getBaseContext(), ClientService.class);
}
//This is called when I click the start button
public void ServiceToggle(View v) {
if (!isServiceRunning(ClientService.class)) {
new Thread(new Runnable() {
@Override
public void run() {
startService(serviceIntent);
}
}).start();
} else {
new Thread(new Runnable() {
@Override
public void run() {
stopService(serviceIntent);
}
}).start();
}
}
ClientService.java
public class ClientService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onCreate();
handler = new Handler();
new Thread(new Client()).start();
return START_STICKY;
}
class Client implements Runnable {
String message;
@Override
public void run() {
try {
socket = new Socket(IP, PORT);
objectInputStream = new ObjectInputStream(socket.getInputStream());
while (true) {
message = (String) objectInputStream.readObject();
UpdateOnUI(message);
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
public void UpdateOnUI(final String string) {
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), string, Toast.LENGTH_SHORT).show();
}
});
}
}
错误来自客户端 (Android) 而不是服务器端 (Windows),并且与 Stack overflow 中提出的其他问题不同。当应用程序从任务中关闭时抛出此错误,服务保持 运行 并且套接字在服务内部实现,关闭 activity 而不是服务时也会出现此错误。
现在可以了。只需要让服务在前台工作。由于一个愚蠢的错误,使它成为 Foreground 之前没有工作,我在另一个线程中进行但忘记执行它(没有调用 .start())。
我制作了一个应用程序,其中有“开始”按钮,当我按下它时,它会启动客户端线程服务,其中客户端套接字通过服务器套接字连接到我的电脑,它运行良好,直到应用程序被清除从最近的名单。当我按下“开始”按钮并执行 finish()
关闭应用程序时,不会发生同样的情况。服务在这两种情况下都运行,但是当我从最近的列表中清除应用程序时,服务保留 运行 但我的服务器应用程序 "Software caused connection abort: socket write error" 收到错误。当我使用 finish()
关闭应用程序时,它也会从最近的列表中清除,但服务会成功保持 运行 服务,而套接字不会出现任何错误。
我该怎么办??
对不起,我的英语不是我的母语。
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent serviceIntent = new Intent(getBaseContext(), ClientService.class);
}
//This is called when I click the start button
public void ServiceToggle(View v) {
if (!isServiceRunning(ClientService.class)) {
new Thread(new Runnable() {
@Override
public void run() {
startService(serviceIntent);
}
}).start();
} else {
new Thread(new Runnable() {
@Override
public void run() {
stopService(serviceIntent);
}
}).start();
}
}
ClientService.java
public class ClientService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onCreate();
handler = new Handler();
new Thread(new Client()).start();
return START_STICKY;
}
class Client implements Runnable {
String message;
@Override
public void run() {
try {
socket = new Socket(IP, PORT);
objectInputStream = new ObjectInputStream(socket.getInputStream());
while (true) {
message = (String) objectInputStream.readObject();
UpdateOnUI(message);
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
public void UpdateOnUI(final String string) {
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), string, Toast.LENGTH_SHORT).show();
}
});
}
}
错误来自客户端 (Android) 而不是服务器端 (Windows),并且与 Stack overflow 中提出的其他问题不同。当应用程序从任务中关闭时抛出此错误,服务保持 运行 并且套接字在服务内部实现,关闭 activity 而不是服务时也会出现此错误。
现在可以了。只需要让服务在前台工作。由于一个愚蠢的错误,使它成为 Foreground 之前没有工作,我在另一个线程中进行但忘记执行它(没有调用 .start())。