【问题标题】:Why does service restart when i kill my app in Android?为什么我在 Android 中杀死我的应用程序时服务会重新启动?
【发布时间】:2015-09-07 11:01:17
【问题描述】:

尝试使用 android 中的服务保持 xmpp 连接处于活动状态

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");
}

}

当我杀死我的应用程序时,我的服务会重新启动。为什么会这样?当我重新启动时,它给我一个异常 NetworkOnMainThreadException at connection = XMPPClient.getConnection(); 当我杀死我的应用程序时,我不希望我的服务重新启动。

【问题讨论】:

    标签: android service xmpp


    【解决方案1】:

    START_STICKY 标志会导致您的服务在销毁时自动重启。您的 onstartCommand 应该返回 START_NOT_STICKY 而不是 START_STICKY。

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

    【讨论】:

    • 我读到的另一件事是 onHandleIntent 是在一个单独的线程上调用的,而不是 ui 线程。怎么做呢?我想我的 onhandleintent 在这里的 ui 线程上被调用。我需要重写 onHandleIntent 方法吗?
    • 我按照你说的做了一切,但是在我杀死我的应用程序后,服务现在根本没有运行
    • 手动杀死您的应用程序总是会杀死您的服务(当您使用 START_STICKY 时也是如此,但是此标志会在服务被杀死时立即重新启动)。如果您不希望您的服务停止,请不要手动终止您的应用,而是使用后退按钮关闭您的应用。
    • 在您的解决方案之后,我的活动甚至不再重新启动
    • 那么您的代码中可能还有其他问题。 START_NOT_STICKY 只会阻止您的 Service 重启,对您的 Activity 没有影响
    【解决方案2】:

    在服务的 onStartCommand(Intent, int, int) 函数中使用 START_NOT_STICKY。

    示例: public static final int START_NOT_STICKY http://developer.android.com/reference/android/app/Service.html

    【讨论】:

    • 我可以再详细一点吗? public static final int START_NOT_STICKY 之后呢?
    • 你不能在 onDestroy() 中使用 public 和 static 修饰符
    • START_NOT_STICKY:从onStartCommand(Intent, int, int)返回的常量:如果该服务的进程在启动时被杀死(从onStartCommand(Intent, int, int)返回后),并且没有新的启动意图传递给它,然后将服务从启动状态中取出,并且在将来显式调用 Context.startService(Intent) 之前不要重新创建。
    猜你喜欢
    • 1970-01-01
    • 2016-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多