Quick Links

Managing Network Usage
Apr 11, 2016

Managing Network Usage

Android application should have fine-gained control over usage of network resources.


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" />
          </intent-filter>
      </activity>
   </application>

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);


Disclaimer
Terms And Conditions Of Use

All content provided on this IT Center blog is for informational purposes only. The owner of this blog makes no representations as to the accuracy or completeness of any information on this site or found by following any link on this site.

The owner of IT Center Blog will not be liable for any errors or omissions in this information nor for the availability of this information. The owner will not be liable for any losses, injuries, or damages from the display or use of this information.

This terms and conditions is subject to change at anytime with or without notice.