简体   繁体   中英

Why does service restart when i kill my app in Android?

Trying to keep an xmpp connection alive using a service in android

public class XMPPService extends Service {

XMPPTCPConnection connection;
String USERNAME;
String PASSWORD;

public XMPPService() {
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    onHandleIntent();
    return START_STICKY;
}

private void onHandleIntent() {
    connection = XMPPClient.getConnection();
    connection.addConnectionListener(
            new AbstractConnectionListener() {
                public void connectionClosed() {
                    Log.i("connection", "closed");

                }

                public void connectionClosedOnError(Exception e) {
                    Log.i("connection", "closed on error");

                }

                public void reconnectionFailed(Exception e) {
                    Log.i("reconnection", "failed");

                }

                public void reconnectionSuccessful() {
                    if (connection.isAuthenticated()) {
                        Log.i("isauthenticauted : ", String.valueOf(connection.isAuthenticated()));
                        Log.i("reconnection", "succesful");
                    } else {
                        try {
                            connection.login(USERNAME, PASSWORD);
                        } catch (XMPPException e) {
                            e.printStackTrace();
                        } catch (SmackException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        Log.i("reconnection", "succesful");
                    }
                }

                public void reconnectingIn(int seconds) {
                    Log.i("reconnectingIn", String.valueOf(seconds));
                }

            }
    );
}



@Override
public void onCreate() {
    Log.i("service", "created");

}

@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    throw new UnsupportedOperationException("Not yet implemented");
}

}

My service restarts when I kill my app. Why is this happening? When I restart it gives me an exception NetworkOnMainThreadException at connection = XMPPClient.getConnection(); I don't want my service to restart when I kill my app.

Make use of START_NOT_STICKY in the onStartCommand(Intent, int, int) function of your service.

example: public static final int START_NOT_STICKYhttp://developer.android.com/reference/android/app/Service.html

The START_STICKY flag causes your service to auto-restart when it's destroyed. Your onstartCommand should return START_NOT_STICKY instead of START_STICKY.

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    onHandleIntent();
    return START_NOT_STICKY;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM