Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others


0 votes
in Technique[技术] by (71.8m points)

java - I would like the internet mandatory in my android' app

I'm looking to impose INTERNET in my android apps that I develop on Android Studio ! it is possible today to play to my app without internet connection and this is a problem for me (about AdMob for sure)! So I would like the internet to be mandatory ! Anyone know the solution ?

thanks a lot... (and sorry for my english i'm french)

Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are able to check the current state of your internet access using the Connectivity Service. Using that service, you can have a callback that will alert you when your internet access situation changes, here's a little example (in Kotlin):

val connectivityManager =
        getApplication<Application>().getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager

    val networkCallback = object : ConnectivityManager.NetworkCallback() {
        override fun onAvailable(network: Network) {
            // Do something

        override fun onLost(network: Network) {
            // Do something else


At the same time, you can also check for wifi using the Wifi Service and data using the Telephony Service. Here's an example where I check both to determine internet situation, though you are free to check for either individually if you see fit:

val wifiManager =
        getApplication<Application>().applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
    val simManager =
        getApplication<Application>().applicationContext.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
    if (wifiManager.connectionInfo.supplicantState != SupplicantState.COMPLETED && simManager.dataState != TelephonyManager.DATA_CONNECTED) {
        // Do something

Keep in mind you will need to enable these permissions in the Android Manifest:

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

Using these services you can selectively disable the app or certain functionalities if the user has no internet.

Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share