Jignesh Ansodariya Jignesh Ansodariya - 7 days ago 6
Android Question

How to get Network Strength in Android Application?

I want to show a network strength of internet in mobile device.

now I am able to check connection from wifi or not using code below, But further I need network Signal Strength. so please tell me what code I need to add?

Thanks!

ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager
.getActiveNetworkInfo();
NetworkInfo mobNetInfo = connectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);



TextView netStatus = (TextView) findViewById(R.id.netStatus);
if (activeNetInfo != null) {
netStatus.setText(" Connection Status - Connected "+activeNetInfo.getTypeName());
} else if (mobNetInfo != null) {
netStatus.setText(" Connection Status - Connected "+mobNetInfo.getTypeName());
} else {
netStatus.setText(" Connection Status - Not Connected ");
}

Answer

Try this code :

ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo Info = cm.getActiveNetworkInfo();
    if (Info == null || !Info.isConnectedOrConnecting()) {
        Log.i(TAG, "No connection");
    } else {
        int netType = Info.getType();
        int netSubtype = Info.getSubtype();

        if (netType == ConnectivityManager.TYPE_WIFI) {
            Log.i(TAG, "Wifi connection");
            WifiManager wifiManager = (WifiManager) getApplication().getSystemService(Context.WIFI_SERVICE);
            List<ScanResult> scanResult = wifiManager.getScanResults();
            for (int i = 0; i < scanResult.size(); i++) {
                Log.d("scanResult", "Speed of wifi"+scanResult.get(i).level);//The db level of signal 
            }


            // Need to get wifi strength
        } else if (netType == ConnectivityManager.TYPE_MOBILE) {
            Log.i(TAG, "GPRS/3G connection");
            // Need to get differentiate between 3G/GPRS
        }
    }
Comments