Android:ConnectivityManager始终返回null

时间:2012-03-12 09:46:57

标签: android

我总是得到一个空指针异常,即使我在这段代码上连接到wifi:

       ConnectivityManager connectivityManager = (ConnectivityManager)
                context.getSystemService(Context.CONNECTIVITY_SERVICE);
    Log.d("Problem", connectivityManager.getActiveNetworkInfo().toString());

我的权限是:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.SEND_SMS" >
</uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_UPDATES" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.CONNECTIVITY_CHANGE" />

我缺少什么:/?我只需要知道他何时连接到wifi点以及何时发生变化。但是连接管理器总是在ActiveNetworkInfo上返回null

5 个答案:

答案 0 :(得分:13)

使用它来了解WIFI是否已连接。

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class Network {
private Context context;
private ConnectivityManager connManager;

public Network(Context ctx) {
    this.context = ctx;
}

public boolean getConnectivityStatus() {
    connManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo info = connManager.getActiveNetworkInfo();
    if (info != null)
        return info.isConnected(); // WIFI connected
    else
        return false; // no info object implies no connectivity
}

}

答案 1 :(得分:1)

connectivityManager.getActiveNetworkInfo()

更新:

返回当前设备的有效网络连接,如果您的设备只有wifi并且未打开wifi,则它始终为空。

建议注册行动接收:ConnectivityManager.CONNECTIVITY_ACTION

收到操作后,您可以使用以下代码检查连接:

NetworkInfo network = mConnectivityManager.getActiveNetworkInfo();
        if (network != null) {
            boolean isConnectedOrConnecting =  network.isConnectedOrConnecting();
            ...
        }

答案 2 :(得分:0)

   //decleration

boolean net;


//OnCreate
net = isOnline();
if (net == true) {
//perform internet related task.
}
else
{

                        AlertDialog.Builder alert = new AlertDialog.Builder(
                                AddSite.this);

                        alert.setMessage(
                                "No signal found or Internet connection is not connected.")
                                .setTitle("Error")
                                .setNeutralButton("OK",
                                        new DialogInterface.OnClickListener() {

                                            public void onClick(
                                                    DialogInterface dialog,
                                                    int which) {
                                                // TODO Auto-generated method
                                                // stub

                                            }
                                        }).show();

}

//function

public boolean isOnline() {
        ConnectivityManager cm = (ConnectivityManager) this
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo activeNetworkInfo = cm.getActiveNetworkInfo();
        return activeNetworkInfo != null;

        // return cm.getActiveNetworkInfo().isConnected();

    }

答案 3 :(得分:0)

public static boolean isNetworkAvailable(Context ctx) {
    NetworkInfo info = getActiveNetworkInfo(ctx);
    return info != null && info.isAvailable();
}

public static boolean isWifiAvailable(Context ctx) {
    NetworkInfo info = getActiveNetworkInfo(ctx);
    return info != null && info.isAvailable()
            && info.getType() == ConnectivityManager.TYPE_WIFI;
}

private static NetworkInfo getActiveNetworkInfo(Context ctx) {
    ConnectivityManager cm = (ConnectivityManager) ctx
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = cm.getActiveNetworkInfo();
    return info;
}

答案 4 :(得分:-1)

您是否已检查 Data Connexion 是否已激活?