First Create New Android Project and Give the Project Name "NetworkCheckDemo".
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/txtviewstatus"
android:text="Dipak Keshariya" android:textSize="24dip"
android:textStyle="bold" android:layout_gravity="center" />
</LinearLayout>
NetworkCheckDemo.java
package com.android.networkcheckdemo;
import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class NetworkCheckDemo extends Activity
{
TextView tvstatus;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tvstatus=(TextView)findViewById(R.id.txtviewstatus);
ConnectivityManager cn=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo nf=cn.getActiveNetworkInfo();
if(nf != null && nf.isConnected()==true )
{
Toast.makeText(this, "Network Available", Toast.LENGTH_LONG).show();
tvstatus.setText("Network Available");
}
else
{
Toast.makeText(this, "Network Not Available", Toast.LENGTH_LONG).show();
tvstatus.setText("Network Not Available");
}
}
}
Add below 3 permissions in Android Manifest File.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
After changes in manifest file your AndroidManifest file look like this.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.drc.Video" android:versionCode="1" android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"></uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".NetworkCheckDemo" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="8" />
</manifest>
Then Save it & Run the Program.
The Output is look like this.
if Connection is Available :-
if Connection is Not Avaiolable :-
then the checking is over and now you can check internet connection successfully using this code
Don’t forget to provide feedback or follow this blog, if you find this blog is useful.
Thank you
ReplyDeleteBut we do I need this permission
it give some exception when apply at handler............
ReplyDeletenice example
ReplyDeleteHow can i get internet available information with wifi, I have a situation where wifi is connected but internet is not available. How can i get that state?
ReplyDeletethanku its worked perfectly and very thankfull to the suggetion.
ReplyDeleteThis blog is nice and very informative. I like this blog.
ReplyDeleteblog Please keep it up.
ReplyDeletepixel car racer mod
It's false that each game which is 3d simply the best ones. 2d Game is likewise well known in the Android gaming market