CellInfoGsm崩溃了应用程序

时间:2016-04-07 11:58:25

标签: android cellinfogsm

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";
}
}

清单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>

任何帮助?

1 个答案:

答案 0 :(得分:0)

试试吧

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";
}