相关文章推荐
活泼的卤蛋  ·  EAGAIN、EWOULDBLOCK、EIN ...·  10 月前    · 
火爆的围巾  ·  Get exclusive jobs on ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

The answer is in your question. Just create a BroadcastReceiver to listen network state, when internet is okay, start server as usual.

public class NetworkBroadcastReceiver extends BroadcastReceiver {
   @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (ConnectivityManager.CONNECTIVITY_ACTION.equals(action)) {
            ConnectivityManager mgr = (ConnectivityManager)
                context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = mgr.getActiveNetworkInfo();
            if(networkInfo != null && networkInfo.isConnected()){
                isNetworkConnected = true;
                //do your work here
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.