In this android example, we will see how to Detect Connected Network Type either Mobile or WIFI in Android Studio by using Kotlin Language. Step 1: Create a new Project in android studio. Go to File > New > New Project > Empty Activity > Next > Enter Name > Select Language Kotlin > Finish Step 2: Go to activity_main.xml file and add the following code <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <TextView <Button Step 3: Add permission to the AndroidManifest.xml file above tag. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> Step 4: Open MainActivity.kt file and add the following code. lateinit var type : TextView check.setOnClickListener{ } // type.text = networkInfo!!.typeName Step 5: Run the app on emulator or real device, you will get the output as in Video activity_main.xml file <?xml version="1.0" encoding="utf-8"?> <TextView <Button MainActivity.kt file import android.content.Context class MainActivity : AppCompatActivity() { check.setOnClickListener{ } // type.text = networkInfo!!.typeName AndroidManifest.xml file <?xml version="1.0" encoding="utf-8"?> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <category android:name="android.intent.category.LAUNCHER" /> Conclusion: In this article we have covered how to detect connected network type in Android Studio by using Kotlin Language.
Implementation:
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Current Network Type"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:id="@+id/type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textColor="@color/black"
android:textStyle="bold" />
android:id="@+id/checkBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Check"></Button>
</LinearLayout>
lateinit var check: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
type = findViewById(R.id.type)
check = findViewById(R.id.checkBtn)
if (checkNetwork()) {
Toast.makeText(this, "Connected With Network", Toast.LENGTH_SHORT).show()
}
else if (!checkNetwork()) {
type.text = "Not Connected"
Toast.makeText(this, "Not Connected", Toast.LENGTH_SHORT).show()
}
}
private fun checkNetwork(): Boolean {
val connManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val networkInfo = connManager.activeNetworkInfo
if(networkInfo!= null){
if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
type.text = networkInfo.typeName
// connected to wifi
return true;
} else{
networkInfo.getType() == ConnectivityManager.TYPE_MOBILE
type.text = networkInfo.typeName
return true
};
}
return false
}
Complete Source Code of Detect Connected Network type Android Example
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Current Network Type"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:id="@+id/type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textColor="@color/black"
android:textStyle="bold" />
android:id="@+id/checkBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Check"></Button>
</LinearLayout>
import android.net.ConnectivityManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
lateinit var type : TextView
lateinit var check: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
type = findViewById(R.id.type)
check = findViewById(R.id.checkBtn)
if (checkNetwork()) {
Toast.makeText(this, "Connected With Network", Toast.LENGTH_SHORT).show()
}
else if (!checkNetwork()) {
type.text = "Not Connected"
Toast.makeText(this, "Not Connected", Toast.LENGTH_SHORT).show()
}
}
private fun checkNetwork(): Boolean {
val connManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val networkInfo = connManager.activeNetworkInfo
if(networkInfo!= null){
if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
type.text = networkInfo.typeName
// connected to wifi
return true;
} else{
networkInfo.getType() == ConnectivityManager.TYPE_MOBILE
type.text = networkInfo.typeName
return true
};
}
return false
}
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.networkType">
<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/Theme.XmlParser">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
</application>
</manifest>
Article Contributed By :
|
|
|
|
291 Views |