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

Saturday 9 July 2022

how to check internet connection in android programmatically kotlin

 how to check internet connection in android programmatically kotlin


Today learn a tutorial on how to check internet connection in android programmatically kotlin

follow this Tutorial and refer site StackOverflow


1.MainActivity.kt:

import android.content.Context

import android.net.ConnectivityManager

import android.net.NetworkCapabilities

import android.os.Build

import androidx.appcompat.app.AppCompatActivity

import android.os.Bundle

import android.widget.Button

import android.widget.Toast


class MainActivity : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)


// register the UI element button

val checkButton: Button = findViewById(R.id.buttonCheck)


// handle the button click to trigger

// checkForInternet function

// and show the Toast message according to it.

checkButton.setOnClickListener {

if (checkForInternet(this)) {

Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show()

} else {

Toast.makeText(this, "Disconnected", Toast.LENGTH_SHORT).show()

}

}

}


private fun checkForInternet(context: Context): Boolean {


// register activity with the connectivity manager service

val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager


// if the android version is equal to M

// or greater we need to use the

// NetworkCapabilities to check what type of

// network has the internet connection

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {


// Returns a Network object corresponding to

// the currently active default data network.

val network = connectivityManager.activeNetwork ?: return false

// Representation of the capabilities of an active network.

val activeNetwork = connectivityManager.getNetworkCapabilities(network) ?: return false


return when {

// Indicates this network uses a Wi-Fi transport,

// or WiFi has network connectivity

activeNetwork.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true

// Indicates this network uses a Cellular transport. or

// Cellular has network connectivity

activeNetwork.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true


// else return false

else -> false

}

} else {

// if the android version is below M

@Suppress("DEPRECATION") val networkInfo =

connectivityManager.activeNetworkInfo ?: return false

@Suppress("DEPRECATION")

return networkInfo.isConnected

}

}

}





2.acttivity_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/buttonCheck"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="CHECK FOR CONNECTION"

android:textColor="@color/white"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintLeft_toLeftOf="parent"

app:layout_constraintRight_toRightOf="parent"

app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>






3.manifest.xml:

 

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.java2blog.helloworldapp">

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

 

    <application

        android:allowBackup="true"

        android:icon="@mipmap/ic_launcher"

        android:label="@string/app_name"

        android:supportsRtl="true"

        android:theme="@style/AppTheme">

        <activity android:name=".HelloWorldActivity">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

 

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

 

</manifest>


No comments:

Post a Comment