ConnectivityManager is crashing while getting network info

我怕爱的太早我们不能终老 提交于 2019-12-12 19:10:25

问题


I am new to Android and when I give the following snippet, my Android application is crashing.

ConnectivityManager manager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); // Application is crashing in this line.

I also checked the AndroidManifest.xml where I gave the Internet permission to the application. Any help would be really useful for my further work.


回答1:


I am not sure what you want to say but if you want to check internet connection then you can use this code

/**
     * THIS IS FUNCTION FOR CHECKING INTERNET CONNECTION
     * @return TRUE IF INTERNET IS PRESENT ELSE RETURN FALSE
     */
    public boolean checkInternetConnection() {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo activeNetworkInfo = cm.getActiveNetworkInfo();
        return (activeNetworkInfo != null && activeNetworkInfo.isAvailable() && activeNetworkInfo.isConnected());
    }

and dont forget

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>



回答2:


<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<uses-permission android:name="android.permission.INTERNET" />


来源:https://stackoverflow.com/questions/6018353/connectivitymanager-is-crashing-while-getting-network-info

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!