Android: Stop/Start service depending on WiFi state?

╄→尐↘猪︶ㄣ 提交于 2019-11-28 16:25:20

You can create a BroadcastReceiver that handles wifi connection changes.

To be more precise, you will want to create a class - say NetWatcher:

public class NetWatcher extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        //here, check that the network connection is available. If yes, start your service. If not, stop your service.
       ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
       NetworkInfo info = cm.getActiveNetworkInfo();
       if (info != null) {
           if (info.isConnected()) {
               //start service
               Intent intent = new Intent(context, MyService.class);
               context.startService(intent);
           }
           else {
               //stop service
               Intent intent = new Intent(context, MyService.class);
               context.stopService(intent);
           }
       }
    }
}

(changing MyService to the name of your service).

Also, in your AndroidManifest, you need to add the following lines:

<receiver android:name="com.example.android.NetWatcher">
     <intent-filter>
          <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
     </intent-filter>
</receiver>

(changing com.example.android to the name of your package).

As @Phil stated, you should extend BroadcastReceiver, and in onReceive method start or stop the service. Something like:

class ConnectionChangeReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
        if (activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
            //start service
        } else {
            //stop service
        }
    }
}

You can make this a private class of your activity, and register receiver on activity create, and unregister it on activity destroy.

More useful information is provided here: Determining and Monitoring the Connectivity Status

Emmanuel Devaux

To start/stop your service when supplicant Wifi state is ok/nok:

  • register a BroadcastReceiver to recieve WIFI state change broadcasted intents
  • inside your BroadCastReceiver check the intent validity then start your service

So register your broadcast receiver to receive WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION. Add permission android.permission.CHANGE_WIFI_STATE or android.permission.ACCESS_NETWORK_STATE. I'm not sure if it is necessary or not.

Then a sample broadcast receiver code could be:

public class MyWifiStatereceiver extends BroadcastReceiver {
    //Other stuff here 

    @Override
    public void onReceive(Context context, Intent intent) {
       Intent srvIntent = new Intent();
       srvIntent.setClass(MyService.class);
       boolean bWifiStateOk = false;

       if (WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION.equals(intent.getAction()) {
           //check intents and service to know if all is ok then set bWifiStateOk accordingly
           bWifiStateOk = ... 
       } else {
           return ; // do nothing ... we're not in good intent context doh !
       }

       if (bWifiStateOk) {
           context.startService(srvIntent);
       } else {
           context.stopService(srvIntent);
       }
    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!