简体   繁体   中英

Android app doesn't work while WiFi is on. (With Firebase and Admob)

My app works perfectly fine with mobile data but it doesn't (Firebase SDKs and Google Admob) when wifi is on.

With FirebaseAuth it doesn't return anything back (no log, no error) after I use fbAuth.signInAnonymously().

With Admob, I get "Network error" message in log when I print loadAdError.message under onAdFailedToLoad() in RewardedAdLoadCallback.

The way I check for internet connection:

private fun hasInternetAccess(): Boolean {
    if (isNetworkAvailable()) {
        try {
            val urlConnection: HttpURLConnection = URL("http://clients3.google.com/generate_204") //call this url to be more efficient instead of google.com
                .openConnection() as HttpURLConnection
            urlConnection.setRequestProperty("User-Agent", "Android")
            urlConnection.setRequestProperty("Connection", "close")
            urlConnection.connectTimeout = 1500
            urlConnection.connect()
            log.error("urlc.responseCode = ${urlConnection.responseCode }")
            return urlConnection.responseCode == 204 &&
                    urlConnection.contentLength == 0
        } catch (e: IOException) {
            Log.e(TAG, "Error checking internet connection", e)
        }
    } else {
        Log.d(TAG, "No network available!")
    }
    return false
}

private fun isNetworkAvailable(): Boolean {
    var result = false
    val connectivityManager = context.getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        val networkCapabilities = connectivityManager.activeNetwork ?: return false
        val actNw = connectivityManager.getNetworkCapabilities(networkCapabilities) ?: return false
        result = when {
            actNw.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true
            actNw.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true
            actNw.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> true
            else -> false
        }
    } else {
        connectivityManager.run {
            connectivityManager.activeNetworkInfo?.run {
                result = when (type) {
                    ConnectivityManager.TYPE_WIFI -> true
                    ConnectivityManager.TYPE_MOBILE -> true
                    ConnectivityManager.TYPE_ETHERNET -> true
                    else -> false
                }
            }
        }
    }
    return result
}

The hasInternetAccess() above returns true whether I use mobile data or wifi and urlc.responseCode is 204. I don't think there's an issue with my wifi as I can browser web and play games, that require internet, just fine.

I have these permissions in manifest:

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

and minSdk is 23, the android device that I test on has version of 6.0. How can I resolve this issue?

This is a known bug with the Android SDK: https://github.com/firebase/firebase-android-sdk/issues/1258

My Android app has been plagued by this problem for years and it seems to be getting worse in 2022. I hope they fix this soon.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM