当我按下系统后退按钮时,我的应用程序断开连接,但我想以这种方式返回:
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
我认为我的Socket Service中存在错误,导致我对此产生疑问:
2018-11-16 11:54:45.869 6099-6099/com.app.ifarma I/SocketService: quit:
2018-11-16 11:54:45.871 6099-6099/com.app.ifarma W/System.err: java.net.SocketException: Socket is closed
2018-11-16 11:54:45.872 6099-6099/com.app.ifarma W/System.err: at java.net.Socket.getInputStream(Socket.java:930)
2018-11-16 11:54:45.872 6099-6099/com.app.ifarma W/System.err: at com.app.ifarma.SocketService.closeConnection(SocketService.java:126)
2018-11-16 11:54:45.872 6099-6099/com.app.ifarma W/System.err: at com.app.ifarma.SocketService.sendMessage(SocketService.java:115)
2018-11-16 11:54:45.872 6099-6099/com.app.ifarma W/System.err: at com.app.ifarma.Scollega$2.run(Scollega.java:71)
2018-11-16 11:54:45.872 6099-6099/com.app.ifarma W/System.err: at android.os.Handler.handleCallback(Handler.java:789)
2018-11-16 11:54:45.872 6099-6099/com.app.ifarma W/System.err: at android.os.Handler.dispatchMessage(Handler.java:98)
2018-11-16 11:54:45.872 6099-6099/com.app.ifarma W/System.err: at android.os.Looper.loop(Looper.java:164)
2018-11-16 11:54:45.872 6099-6099/com.app.ifarma W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6541)
2018-11-16 11:54:45.872 6099-6099/com.app.ifarma W/System.err: at java.lang.reflect.Method.invoke(Native Method)
2018-11-16 11:54:45.872 6099-6099/com.app.ifarma W/System.err: at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
2018-11-16 11:54:45.873 6099-6099/com.app.ifarma W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
2018-11-16 11:54:45.886 6099-6134/com.app.ifarma W/System.err: java.net.SocketException: Socket closed
2018-11-16 11:54:45.886 6099-6134/com.app.ifarma W/System.err: at java.net.SocketInputStream.socketRead0(Native Method)
2018-11-16 11:54:45.887 6099-6134/com.app.ifarma W/System.err: at java.net.SocketInputStream.socketRead(SocketInputStream.java:114)
2018-11-16 11:54:45.887 6099-6134/com.app.ifarma W/System.err: at java.net.SocketInputStream.read(SocketInputStream.java:170)
2018-11-16 11:54:45.887 6099-6134/com.app.ifarma W/System.err: at java.net.SocketInputStream.read(SocketInputStream.java:139)
2018-11-16 11:54:45.887 6099-6134/com.app.ifarma W/System.err: at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:288)
2018-11-16 11:54:45.889 6099-6134/com.app.ifarma W/System.err: at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:351)
2018-11-16 11:54:45.894 6099-6134/com.app.ifarma W/System.err: at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:180)
2018-11-16 11:54:45.898 6099-6134/com.app.ifarma W/System.err: at java.io.InputStreamReader.read(InputStreamReader.java:184)
2018-11-16 11:54:45.899 6099-6134/com.app.ifarma W/System.err: at java.io.BufferedReader.fill(BufferedReader.java:172)
2018-11-16 11:54:45.900 6099-6134/com.app.ifarma W/System.err: at java.io.BufferedReader.readLine(BufferedReader.java:335)
2018-11-16 11:54:45.905 6099-6134/com.app.ifarma W/System.err: at java.io.BufferedReader.readLine(BufferedReader.java:400)
2018-11-16 11:54:45.905 6099-6134/com.app.ifarma W/System.err: at com.app.ifarma.SocketService$ClientThread.run(SocketService.java:272)
2018-11-16 11:54:45.906 6099-6134/com.app.ifarma W/System.err: at java.lang.Thread.run(Thread.java:764)
2018-11-16 11:54:45.907 6099-6131/com.app.ifarma W/System.err: java.net.SocketException: Socket closed
2018-11-16 11:54:45.907 6099-6131/com.app.ifarma W/System.err: at java.net.SocketInputStream.socketRead0(Native Method)
2018-11-16 11:54:45.908 6099-6131/com.app.ifarma W/System.err: at java.net.SocketInputStream.socketRead(SocketInputStream.java:114)
2018-11-16 11:54:45.909 6099-6131/com.app.ifarma W/System.err: at java.net.SocketInputStream.read(SocketInputStream.java:170)
2018-11-16 11:54:45.909 6099-6131/com.app.ifarma W/System.err: at java.net.SocketInputStream.read(SocketInputStream.java:139)
2018-11-16 11:54:45.909 6099-6131/com.app.ifarma W/System.err: at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:288)
2018-11-16 11:54:45.910 6099-6131/com.app.ifarma W/System.err: at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:351)
2018-11-16 11:54:45.918 6099-6131/com.app.ifarma W/System.err: at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:180)
2018-11-16 11:54:45.918 6099-6131/com.app.ifarma W/System.err: at java.io.InputStreamReader.read(InputStreamReader.java:184)
2018-11-16 11:54:45.920 6099-6131/com.app.ifarma W/System.err: at java.io.BufferedReader.fill(BufferedReader.java:172)
2018-11-16 11:54:45.920 6099-6131/com.app.ifarma W/System.err: at java.io.BufferedReader.readLine(BufferedReader.java:335)
2018-11-16 11:54:45.921 6099-6131/com.app.ifarma W/System.err: at java.io.BufferedReader.readLine(BufferedReader.java:400)
2018-11-16 11:54:45.922 6099-6131/com.app.ifarma W/System.err: at com.app.ifarma.SocketService$ClientThread.run(SocketService.java:272)
2018-11-16 11:54:45.923 6099-6131/com.app.ifarma W/System.err: at java.lang.Thread.run(Thread.java:764)
2018-11-16 11:54:45.977 6099-6099/com.app.ifarma I/SocketService: quit:
2018-11-16 11:54:45.978 6099-6099/com.app.ifarma W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.OutputStream java.net.Socket.getOutputStream()' on a null object reference
2018-11-16 11:54:45.979 6099-6099/com.app.ifarma W/System.err: at com.app.ifarma.SocketService.closeConnection(SocketService.java:125)
2018-11-16 11:54:45.979 6099-6099/com.app.ifarma W/System.err: at com.app.ifarma.SocketService.sendMessage(SocketService.java:115)
2018-11-16 11:54:45.979 6099-6099/com.app.ifarma W/System.err: at com.app.ifarma.Scollega$2.run(Scollega.java:71)
2018-11-16 11:54:45.980 6099-6099/com.app.ifarma W/System.err: at android.os.Handler.handleCallback(Handler.java:789)
2018-11-16 11:54:45.980 6099-6099/com.app.ifarma W/System.err: at android.os.Handler.dispatchMessage(Handler.java:98)
2018-11-16 11:54:45.980 6099-6099/com.app.ifarma W/System.err: at android.os.Looper.loop(Looper.java:164)
2018-11-16 11:54:45.980 6099-6099/com.app.ifarma W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6541)
2018-11-16 11:54:45.980 6099-6099/com.app.ifarma W/System.err: at java.lang.reflect.Method.invoke(Native Method)
2018-11-16 11:54:45.980 6099-6099/com.app.ifarma W/System.err: at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
2018-11-16 11:54:45.981 6099-6099/com.app.ifarma W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
这是我的socketService(问题部分):
@Override
public void onCreate() {
super.onCreate();
}
public void sendMessage(String message) {
line = "";
if (out != null && !out.checkError()) {
out.println(message);
out.flush();
Log.i("SocketService", "" + message.toString());
//ci aspettiamo una risposta solo se non e' quit
if (message.equals("quit:")) {
closeConnection();
}
}
}
public void closeConnection() {
try {
connect = false;
socket.getOutputStream().close();
socket.getInputStream().close();
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
socket = null;
}
也许我的“ scollega”课程也有问题
public class Scollega extends Activity {
public SocketService s;
//service
boolean mBounded;
ServiceConnection mConnection = new ServiceConnection() {
public void onServiceDisconnected(ComponentName name) {
mBounded = false;
s = null;
}
public void onServiceConnected(ComponentName name, IBinder service) {
mBounded = true;
LocalBinder mLocalBinder = (LocalBinder) service;
s = mLocalBinder.getServerInstance();
}
};
;
@Override
protected void onStart() {
super.onStart();
Intent mIntent = new Intent(this, SocketService.class);
bindService(mIntent, mConnection, BIND_AUTO_CREATE);
}
@Override
protected void onStop() {
super.onStop();
if (mBounded) {
unbindService(mConnection);
mBounded = false;
}
}
;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loader);
getWindow().setWindowAnimations(0);
TextView outputView = (TextView) findViewById(R.id.textLoader);
outputView.setText("Scollegamento...");
//creiamo il timeout dopo l'avvio del servizio
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
try {
s.sendMessage("quit:");
} catch (Exception e) {
e.printStackTrace();
}
//torniamo in Home
Intent i = new Intent(Scollega.this, Home.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
overridePendingTransition(R.anim.slide_left, R.anim.slide_right);
finish();
}
}, 1000); // ritardiamo l'esecuzione di un secondo
}
我从一个不想再处理它的人那里继承了这个程序,我解决了大多数问题,但是我不知道该怎么做...