Wednesday, May 15, 2019

Check internet connection



In android we  have to build applications which require internet therefore there is need to check whether the internet in connected to device or not. I have written a simple example to check the internet connection the code is given below:



when internet is OFF
when internet is ON









































Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  package="com.apkglobal.blog_connection">

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

    <application   
     android:allowBackup="true"        
     android:icon="@mipmap/ic_launcher"       
     android:label="@string/app_name"       
     android:roundIcon="@mipmap/ic_launcher_round"    
     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>
    </application>

</manifest>

MainActivity.java
public class MainActivity extends AppCompatActivity {
    Context _context;


    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        ConnectivityManager connectivity = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null) {
                for (int i = 0; i < info.length; i++) {
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {

                        Toast.makeText(this, "Internet is connected", Toast.LENGTH_SHORT).show();

                    }
                    else {
                        Toast.makeText(this, "No internet", Toast.LENGTH_SHORT).show();
                    }
                }
            }

  }
 else {
            Toast.makeText(this, "No internet", Toast.LENGTH_SHORT).show();
        }
    }
}
       

activitymain.xml


<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
   xmlns:android="http://schemas.android.com/apk/res/android" 
   xmlns:app="http://schemas.android.com/apk/res-auto"  
   xmlns:tools="http://schemas.android.com/tools"    
   android:layout_width="match_parent"   
   android:layout_height="match_parent" 
   tools:context=".MainActivity">

    <TextView       
    android:layout_width="wrap_content"     
    android:layout_height="wrap_content"   
    android:text="Hello World!"        
       app:layout_constraintBottom_toBottomOf="parent"  
       app:layout_constraintLeft_toLeftOf="parent" 
       app:layout_constraintRight_toRightOf="parent" 
       app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>




No comments:

Alert Dialog Box

Alert Dialog Box Android AlertDialog   can be used to display the dialog message with OK and Cancel buttons. It can be used to inter...