0
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.telephony.CellInfoGsm;
import android.telephony.CellSignalStrengthGsm;
import android.telephony.TelephonyManager;
import android.widget.Toast;

public class ConnectivityChangeReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
   String status= getConnectivityStatus(context);
    Toast.makeText(context, status, Toast.LENGTH_LONG).show();
}


public String getConnectivityStatus(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();

    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    int nt = tm.getNetworkType();

    if (null != activeNetwork) {
        if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
            WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
            int linkSpeed = wifiManager.getConnectionInfo().getRssi();
            return "TYPE_WIFI" + linkSpeed;
        }
        if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {

            //throws null pointer exception here
            CellInfoGsm cellinfogsm = (CellInfoGsm) tm.getAllCellInfo().get(0);
            CellSignalStrengthGsm cellSignalStrengthGsm = cellinfogsm.getCellSignalStrength();
            int dbm = cellSignalStrengthGsm.getLevel();
            return "TYPE_MOBILE " + nt + dbm;
        }
    }
    return "TYPE_NOT_CONNECTED";
}
}

Manifest xml-

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.imslbd.connectivitymanagertestone">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name=".ConnectivityChangeReceiver">
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
        </intent-filter>
    </receiver>
</application>

Any help?

s.k.paul
  • 7,099
  • 28
  • 93
  • 168
  • Surely a user with 6 gold badges ought to know better than to post a code dump with a title? Where are the logs? Where is the analysis from your own debugging efforts? – Michael Apr 07 '16 at 12:02
  • @Michael, I am a very new comer to the android world, also in java. The above code I found from google and just exploring it. – s.k.paul Apr 07 '16 at 12:06
  • My comment wasn't really related to your knowledge of Android development, but to knowing how to post a good question on StackOverflow - i.e. one that is clear, useful, and shows research effort. – Michael Apr 07 '16 at 12:15
  • Where is your crash report ? Post full stacktrace. – Piyush Apr 07 '16 at 12:58
  • @PiyushGupta "java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.util.List.iterator()' on a null object reference" – s.k.paul Apr 07 '16 at 13:04
  • Then error is not in this code.. Show the line at which u r getting error? – Piyush Apr 07 '16 at 13:04
  • @PiyushGupta, Read the question. It's already mentioned there. – s.k.paul Apr 07 '16 at 13:08

1 Answers1

0

Try it

public String getConnectivityStatus(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();

    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    int nt = tm.getNetworkType();

    if (null != activeNetwork) {
        if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
            WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            int linkSpeed = wifiManager.getConnectionInfo().getRssi();
            return "TYPE_WIFI" + linkSpeed;
        }
        if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {

            int dbm = -192;

            try {
                for (final CellInfo info : tm.getAllCellInfo()) {
                    if (info instanceof CellInfoGsm) {
                        final CellSignalStrengthGsm gsm = ((CellInfoGsm) info).getCellSignalStrength();
                        // do what you need

                        dbm = gsm.getLevel();
                    } else if (info instanceof CellInfoCdma) {
                        final CellSignalStrengthCdma cdma = ((CellInfoCdma) info).getCellSignalStrength();
                        // do what you need
                        dbm = cdma.getLevel();
                    } else if (info instanceof CellInfoLte) {
                        final CellSignalStrengthLte lte = ((CellInfoLte) info).getCellSignalStrength();
                        // do what you need
                        dbm = lte.getLevel();
                    } else {
                        throw new Exception("Unknown type of cell signal!");
                    }
                }
            } catch (Exception e) {
                Log.e(TAG, "Unable to obtain cell signal information", e);
            }

            return "TYPE_MOBILE " + nt + dbm;
        }
    }
    return "TYPE_NOT_CONNECTED";
}
Gorio
  • 1,606
  • 2
  • 19
  • 32
  • Throws exception "java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.util.List.iterator()' on a null object reference" – s.k.paul Apr 07 '16 at 13:03
  • What is your Android API Level ? API Level 17 is required. Could you tell me where error occurs ? – Gorio Apr 07 '16 at 13:14
  • -CellInfoGsm cellinfogsm = (CellInfoGsm) tm.getAllCellInfo().get(0); – s.k.paul Apr 07 '16 at 13:18
  • @s.k.paul My solution, doesn't have this line.. Use my solution.. replace your method getConnectivityStatus to mine – Gorio Apr 07 '16 at 13:20
  • for (final CellInfo info : tm.getAllCellInfo()) {...} – s.k.paul Apr 09 '16 at 09:52