2016-04-07 26 views
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-CellInfoGsm stürzt die App

<?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> 

Jede Hilfe?

+0

Sicherlich ein Benutzer mit 6 Gold Abzeichen soll besser wissen, als ein Code-Dump mit einem Titel zu schreiben? Wo sind die Protokolle? Wo liegt die Analyse aus Ihren eigenen Debugging-Bemühungen? – Michael

+0

@Michael, ich bin ein sehr neuer Anwärter auf die Android-Welt, auch in Java. Der obige Code, den ich von Google gefunden habe und ihn gerade erkunde. –

+0

Mein Kommentar bezog sich nicht auf Ihr Wissen über Android-Entwicklung, sondern darauf, eine gute Frage zu StackOverflow zu stellen - d. H. Eine, die klar und nützlich ist und Forschungsaufwand zeigt. – Michael

Antwort

0

Probieren Sie es

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

Auslöser "java.lang.NullPointerException: Versuch, die Schnittstellenmethode aufzurufen" java.util.Iterator java.util.List.iterator() "für einen Nullobjektverweis" –

+0

Was ist Ihr Android-API-Level? API Level 17 ist erforderlich. Können Sie mir sagen, wo ein Fehler auftritt? – Gorio

+0

-CellInfoGsm cellinfogsm = (CellInfoGsm) tm.getAllCellInfo(). Get (0); –