سؤال

I know it's not possible to keep the same socket across network changes, but what is the easiest way to restart a service after a network change? I assume the best practice is to listen for a android.net.conn.CONNECTIVITY_CHANGE broadcast, but where would I put this code? Can a service kill itself and recreate itself from scratch so my TCP connection will restart too? Or do I have to put code into another service that kills the TCP service (if so, how do I find the running instance of the service to kill it?)

Thank you for an answer.

هل كانت مفيدة؟

المحلول

EDITED: Kill your service just like you starting it.

In your broadcast receiver:

 stopService(new Intent(context, ServiceName.class));
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top