Download data when internet connectivity is detected

后端 未结 2 513
[愿得一人]
[愿得一人] 2021-01-28 03:44

I want to download some data including json and image(binary data) whenever internet connectivity is available.

I have a full working code which can do that. The problem

2条回答
  •  借酒劲吻你
    2021-01-28 04:08

    in the android developer guide mentioned you can Monitor for Changes in Connectivity

    1st - you create a NetworkChangeReceiver class that is a BroadCastReceive , where you implement your code to download what you want :

    public class NetworkChangeReceiver extends BroadcastReceiver {
    
    @Override
    public void onReceive(final Context context, final Intent intent) {
    
        final ConnectivityManager connMgr = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
    
        final NetworkInfo wifi = connMgr
                .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    
        NetworkInfo activeNetwork = connMgr.getActiveNetworkInfo(); 
    
        boolean isConnected = wifi != null &&   
                    wifi.isConnected() ;
    
        if (isConnected ) {
            // Do something
    
            Log.d("Netowk Available ", "Flag No 1");
        }
      }
    }
    

    2nd - you register your BroadCastReceiver in the manifest file like the following :

     
                
                    
    
                
     
    

提交回复
热议问题