Blog purpose for android basic example for android app developer. any query please my contact

Tuesday, 9 March 2021

how to check internet connection in kotlin programmatically

 how to check internet connection in kotlin programmatically 

In this Article Today Learn how to check internet connection in kotlin programmatically . kotlin is aprogramming langauge follow the Method:

  check_connection.setOnClickListener {

            connectivity = context.getSystemService(Service.CONNECTIVITY_SERVICE)

                    as ConnectivityManager

            if ( connectivity != null)

            {

                info = connectivity!!.activeNetworkInfo


                if (info != null)

                {

                    if (info!!.state == NetworkInfo.State.CONNECTED)

                    {

                        Toast.makeText(context, "CONNECTED", Toast.LENGTH_LONG).show()

                    }

                }

                else

                {

                    Toast.makeText(context, "NOT CONNECTED", Toast.LENGTH_LONG).show()

                }

            }

        }

    }


Modify AndroidManifest.xml:

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

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


full Source Code Provide Follow this Steps:


1.Open Android Studio Create a New Project:


how to check internet connection in kotlin programmatically



2.Select A Empty Activity and Cick Next


how to check internet connection in kotlin programmatically



3.Type a Project Name and Select a 'Kotlin' and Click Finsih:


how to check internet connection in kotlin programmatically



follow the Full Source Code how to check internet connection in kotlin programmatically:


1.MainActivity.kt:


package com.akash.internetconnection

import android.app.Service
import android.net.ConnectivityManager
import android.net.NetworkInfo
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {

    var context = this
    var connectivity : ConnectivityManager? = null
    var info : NetworkInfo? = null

    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        check_connection.setOnClickListener {

            connectivity = context.getSystemService(Service.CONNECTIVITY_SERVICE)
                    as ConnectivityManager

            if ( connectivity != null)
            {
                info = connectivity!!.activeNetworkInfo

                if (info != null)
                {
                    if (info!!.state == NetworkInfo.State.CONNECTED)
                    {
                        Toast.makeText(context, "CONNECTED", Toast.LENGTH_LONG).show()
                    }
                }
                else
                {
                    Toast.makeText(context, "NOT CONNECTED", Toast.LENGTH_LONG).show()
                }
            }
        }
    }
}


2.activity_main.xml:


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">


    <Button

        android:id="@+id/check_connection"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginLeft="19dp"
        android:layout_marginTop="15dp"
        android:layout_marginRight="19dp"
        android:background="@color/colorPrimary"
        android:padding="4dp"
        android:text="CHECK CONNECTION"
        android:textColor="#FFFFFF"
        android:textSize="20sp" />
</androidx.constraintlayout.widget.ConstraintLayout>


3.AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.akash.internetconnection">
    <uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <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>


4.color.xml:


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#6200EE</color>
    <color name="colorPrimaryDark">#3700B3</color>
    <color name="colorAccent">#03DAC5</color>
</resources>


5.strings.xml:


<resources>
    <string name="app_name">Internet Connection</string>
</resources>

6.Output:


how to check internet connection in kotlin programmatically



how to check internet connection in kotlin programmatically














No comments:

Post a Comment