Managing Network Usage

Connecting to the Network on Android Devices
February 4, 2015
My computer is running slow. What steps can I do to fix it?
September 26, 2016

Android application should have fine-gained control over usage of network resources. Should allowed users to control how often app syncs data, whether to perform uploads/downloads only when on Wi-Fi, whether to use data while roaming, and so on. With this controls apps will not drain battery and users Internet Limits.
For this controls developers must add below code sample in the Manifest file.

 <application ...> ... 
       <activity android:label="SettingsActivity" android:name=".SettingsActivity"> 
          <intent-filter> <action android:name="android.intent.action.MANAGE_NETWORK_USAGE" /> 
             <category android:name="android.intent.category.DEFAULT" /> 

For managing network, there are two different classes in android packages. ConnectivityManager and NetworkInfo. These two classes monitor network connections, send broadcast, describes the status.
This part of code tests network connectivity. Both checks Wi-Fi and mobile and informs which type of network is connected.

   private static final String DEBUG_TAG = "NetworkStatusExample"; 
        ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
        NetworkInfo networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 
        boolean isWifiConn = networkInfo.isConnected(); 
        networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 
        boolean isMobileConn = networkInfo.isConnected(); 
        Log.d(DEBUG_TAG, "Wifi connected: " + isWifiConn); 
        Log.d(DEBUG_TAG, "Mobile connected: " + isMobileConn);


Comments are closed.