-1

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.

BrechtDeMan
  • 6,589
  • 4
  • 24
  • 25
anupam x
  • 779
  • 2
  • 7
  • 12

2 Answers2

0

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

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

Suchi Johari
  • 109
  • 7
  • can i have a little more detail? public static final int START_NOT_STICKY what after this? – anupam x Sep 07 '15 at 11:30
  • and you cant have public and static modifiers in onDestroy() – anupam x Sep 07 '15 at 11:32
  • START_NOT_STICKY: It is a constant to return from onStartCommand(Intent, int, int): if this service's process is killed while it is started (after returning from onStartCommand(Intent, int, int)), and there are no new start intents to deliver to it, then take the service out of the started state and don't recreate until a future explicit call to Context.startService(Intent). – Suchi Johari Sep 07 '15 at 17:27
0

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;
}
Frank D.
  • 1,306
  • 1
  • 13
  • 23
  • another thing i read that onHandleIntent is called on a seperate thread than the ui thread. how to do that also? i guess my onhandleintent is getting called on ui thread here. DO i need to override the onHandleIntent method? – anupam x Sep 07 '15 at 11:22
  • i did everything like you said but after i kill my app the service is not running at all now – anupam x Sep 07 '15 at 11:27
  • Killing your app manually always kills your Service (also when you use START_STICKY, however this flag restarts the Service immediately when it's killed). If you don't want your service to stop, don't kill your app manually but use the back button to close your app instead. – Frank D. Sep 07 '15 at 11:32
  • after your solution my activity is not even restarting anymore – anupam x Sep 07 '15 at 11:35
  • Then there is probably something else wrong in your code. START_NOT_STICKY only prevents your Service from restarting and has no effect on your Activity – Frank D. Sep 07 '15 at 11:36
  • Also, you referred to onHandleIntent, which is only defined for IntentService, not the Service class you are currently using. http://developer.android.com/reference/android/app/IntentService.html#onHandleIntent(android.content.Intent) – Frank D. Sep 07 '15 at 11:37
  • yeah its posted right above these comments if you could see. Do you find anything wrong? – anupam x Sep 07 '15 at 11:38
  • You probably should be overriding onHandleIntent, however you can't at the moment because you are using the Service class instead of IntentService. Maybe it can also work if you try AsyncTask, check out this topic: http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception – Frank D. Sep 07 '15 at 11:42
  • so i should use intentservice for keeping my xmpp connection alive? – anupam x Sep 07 '15 at 11:43
  • I found these suggestions using a Google search. I don't have your full code so I don't know if this will solve your problem, but I recommend you to a Google search yourself and try to learn from other people's solutions. – Frank D. Sep 07 '15 at 11:46
  • ok i need to create an android service to keep my xmpp connection alive. Im using smakc can you guide me or give me a link which might be helpful?? – anupam x Sep 07 '15 at 11:53
  • I'm not familiar with smakc. Please try a Google/stackoverflow search or ask a new question on stackoverflow. – Frank D. Sep 07 '15 at 12:03