我是初学者,试图在android中学习service
....当我运行以下代码时,它在应用程序处于活动状态时正常工作。但是,在从最近的应用程序列表中关闭应用程序后,其显示对话框"不幸的是,(appname)已停止。"最多3次然后停止。但是,当我没有打电话给create_socket()
时,它正常工作,显示log
值i
.....请帮助。
package com.example.runwaylabz.canvi;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.IBinder;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.util.Log;
import org.apache.commons.io.IOUtils;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import xdroid.toaster.Toaster;
public class SocketConnection extends Service {
private static Socket socket;
private static InputStream inputStream;
private static OutputStream outputStream;
private static String ip;
private static int port;
private static SocketConnection socketConnection = null;
public SocketConnection() {
super();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
AsyncTask asyncTask = new AsyncTask() {
@Override
protected Object doInBackground(Object[] objects) {
new Thread(new Runnable() {
@Override
public void run() {
int i = 1;
while (i > 0) {
Log.i("HI", "" + i);
i++;
}
}
}).start();
create_socket();
return null;
}
};
asyncTask.execute();
return START_STICKY;
}
private static class instancehelper {
private static final SocketConnection Instance = new SocketConnection();
}
public static SocketConnection getInstance(String ip, int port) {
socketConnection = instancehelper.Instance;
socketConnection.setIp(ip);
socketConnection.setPort(port);
return socketConnection;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
public void create_socket() {
new Thread(new Runnable() {
@Override
public void run() {
try {
socket = new Socket(ip, port);
if(socket.isConnected()){
Log.w("debug","socket created");
}
} catch (IOException e) {
e.printStackTrace();
}
try {
inputStream = socket.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
try {
outputStream = socket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
public void close_socket(){
new Thread(new Runnable() {
@Override
public void run() {
IOUtils.closeQuietly(inputStream);
inputStream=null;
if(inputStream==null){
Log.w("debug","inputStream Closed");
}
else {
Log.w("debug","inputStreaam not Closed");
}
while(inputStream==null){
IOUtils.closeQuietly(inputStream);
}
IOUtils.closeQuietly(outputStream);
outputStream=null;
if(outputStream==null){
Log.w("debug","outputStream Closed");
}
else {
Log.w("debug","outputStreaam not Closed");
}
while(outputStream==null){
IOUtils.closeQuietly(outputStream);
}
try {
socket.close();
socket=null;
} catch (IOException e) {
if(!socket.isClosed()){
try {
socket.close();
socket=null;
} catch (IOException e1) {
e1.printStackTrace();
}
}
e.printStackTrace();
}
if(socket==null){
Log.w("debug","Socket Closed");
}
else {
Log.w("debug","Socket not Closed");
}
}
}).start();
}
public static void writeouputstream(final String outputstreamString) {
new Thread(new Runnable() {
@Override
public void run() {
try {
IOUtils.write(("12345678" + outputstreamString).getBytes("ASCII"), outputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
public void setPort(int port) {
this.port = port;
}
public void setIp(String ip) {
this.ip = ip;
}
public static void setSocket(Socket socket) {
SocketConnection.socket = socket;
}
public static void setOutputStream(OutputStream outputStream) {
SocketConnection.outputStream = outputStream;
}
public static void setInputStream(InputStream inputStream) {
SocketConnection.inputStream = inputStream;
}
public static InputStream getInputStream() {
return inputStream;
}
public int getPort() {
return port;
}
public static OutputStream getOutputStream() {
return outputStream;
}
public static Socket getSocket() {
return socket;
}
public String getIp() {
return ip;
}
@Override
public void onTaskRemoved(Intent rootIntent) {
close_socket();
Intent restartService = new Intent(getApplicationContext(), getInstance(ip,port).getClass());
restartService.setPackage(getPackageName());
PendingIntent restartServicePI = PendingIntent.getService(
getApplicationContext(), 1, restartService,
PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmService = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() +1000, restartServicePI);
}
}
关闭应用程序后的logcat是
39 26530-26558/com.example.runwaylabz.canvi W/System.err: java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 0) after 90000ms: isConnected failed: ECONNREFUSED (Connection refused)
08-01 20:21:32.839 26530-26558/com.example.runwaylabz.canvi W/System.err: at libcore.io.IoBridge.isConnected(IoBridge.java:267)
08-01 20:21:32.839 26530-26558/com.example.runwaylabz.canvi W/System.err: at libcore.io.IoBridge.connectErrno(IoBridge.java:191)
08-01 20:21:32.839 26530-26558/com.example.runwaylabz.canvi W/System.err: at libcore.io.IoBridge.connect(IoBridge.java:127)
08-01 20:21:32.840 26530-26558/com.example.runwaylabz.canvi W/System.err: at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:188)
08-01 20:21:32.840 26530-26558/com.example.runwaylabz.canvi W/System.err: at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:168)
08-01 20:21:32.840 26530-26558/com.example.runwaylabz.canvi W/System.err: at java.net.Socket.startupSocket(Socket.java:609)
08-01 20:21:32.840 26530-26558/com.example.runwaylabz.canvi W/System.err: at java.net.Socket.tryAllAddresses(Socket.java:137)
08-01 20:21:32.840 26530-26558/com.example.runwaylabz.canvi W/System.err: at java.net.Socket.<init>(Socket.java:187)
08-01 20:21:32.840 26530-26558/com.example.runwaylabz.canvi W/System.err: at java.net.Socket.<init>(Socket.java:159)
08-01 20:21:32.840 26530-26558/com.example.runwaylabz.canvi W/System.err: at com.example.runwaylabz.canvi.SocketConnection$2.run(SocketConnection.java:105)
08-01 20:21:32.840 26530-26558/com.example.runwaylabz.canvi W/System.err: at java.lang.Thread.run(Thread.java:818)
08-01 20:21:32.840 26530-26558/com.example.runwaylabz.canvi W/System.err: Caused by: android.system.ErrnoException: isConnected failed: ECONNREFUSED (Connection refused)
08-01 20:21:32.840 26530-26558/com.example.runwaylabz.canvi W/System.err: at libcore.io.IoBridge.isConnected(IoBridge.java:252)
08-01 20:21:32.840 26530-26558/com.example.runwaylabz.canvi W/System.err: ... 10 more
08-01 20:21:32.841 26530-26558/com.example.runwaylabz.canvi E/AndroidRuntime: FATAL EXCEPTION: Thread-29548
Process: com.example.runwaylabz.canvi, PID: 26530
java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.InputStream java.net.Socket.getInputStream()' on a null object reference
at com.example.runwaylabz.canvi.SocketConnection$2.run(SocketConnection.java:115)
at java.lang.Thread.run(Thread.java:818)