当我重新启动应用程序时,会运行新服务

时间:2017-09-09 18:54:46

标签: android

我在我的应用程序中创建了一个服务类,以便在后台运行一些任务。当我关闭应用程序时,服务仍在运行,它没问题。但问题是,当我再次运行应用程序时,运行一个新服务,每次重新启动应用程序时都会创建一个新服务。这是我的代码

public class myService extends Service {

Socket socket;


@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}


@Override
public int onStartCommand(Intent intent, int flags, int startId) {


    connectSocket();

    return START_STICKY;
}

@Override
public void onDestroy() {
    super.onDestroy();

    socket.disconnect();
}


private void connectSocket() {
    try {

        socket = IO.socket("http://192.168.1.52:2500");
        socket.on(Socket.EVENT_CONNECT, onConnected);
        socket.on(Socket.EVENT_CONNECT_ERROR, onConnectionError);
        socket.on(Socket.EVENT_CONNECT_TIMEOUT, onConnectionTimout);
        socket.on(Socket.EVENT_DISCONNECT, onDisconnect);
        socket.on("test_callback", eventCallback );
        socket.connect();
    } catch (URISyntaxException e) {
        Log.d(getClass().getSimpleName(), "Socket Connection Exception");
        e.printStackTrace();
    }
}

private Emitter.Listener onConnected = new Emitter.Listener() {
    @Override
    public void call(Object... args) {
        Log.d("SocketIO","onConnected");
        socket.connect();
    }
};

private Emitter.Listener onConnectionError = new Emitter.Listener() {
    @Override
    public void call(Object... args) {
        Log.d("SocketIO","onConnectionError");
            socket.connect();
    }
};


private Emitter.Listener onConnectionTimout = new Emitter.Listener() {
    @Override
    public void call(Object... args) {
        Log.d("SocketIO","onConnectionTimout");
            socket.connect();
    }
};


private Emitter.Listener onDisconnect = new Emitter.Listener() {
    @Override
    public void call(Object... args) {
        Log.d("SocketIO","onDisconnect");
            socket.disconnect();
    }
};


int notno = 0;
private Emitter.Listener eventCallback = new Emitter.Listener() {
    @Override
    public void call(Object... args) {
        JSONObject data = (JSONObject) args[0];
        String message;
        try {
            message = data.getString("message").toString();
            NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(myService.this)
                            .setSmallIcon(R.drawable.head)
                            .setContentTitle("Message")
                            .setContentText(message);
            NotificationManager nfm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
            nfm.notify(notno ,mBuilder.build());
            notno++;

        } catch (JSONException ignored) {

        }
    }
};

}

并在主类的onCreate方法中运行此代码:

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    startService(new Intent(getBaseContext(), myService.class));

}

1 个答案:

答案 0 :(得分:1)

这是设计的。每次调用startService时,都会调用onStartCommand。这允许它以不同的意图启动。但是它只会被创建一次(当然,除非资源停止)。如果你只想拥有一个套接字或线程的单个实例,你的工作就是确保你第一次调用onStartCommand时只实例化它们。

此外,您的代码不起作用。默认情况下,服务没有自己的线程或进程。这意味着您需要启动一个新线程来完成套接字工作,否则您将获得NetworkOnMainThreadErrror。