ConnectivityManager is crashing while getting network info

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.

-Thanks, Ron..

Answer1:

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>

Answer2:

<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" />

人吐槽 人点赞

Recommend

Comment

用户名: 密码:
验证码: 匿名发表

你可以使用这些语言

查看评论:ConnectivityManager is crashing while getting network info