简体   繁体   English

当 WiFi 开启时,Android 应用程序无法运行。 (使用 Firebase 和 Admob)

[英]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.我的应用程序在移动数据上运行良好,但在 wifi 开启时(Firebase SDK 和 Google Admob)不能。

With FirebaseAuth it doesn't return anything back (no log, no error) after I use fbAuth.signInAnonymously().使用 FirebaseAuth 在我使用 fbAuth.signInAnonymously() 后,它不会返回任何内容(没有日志,没有错误)。

With Admob, I get "Network error" message in log when I print loadAdError.message under onAdFailedToLoad() in RewardedAdLoadCallback.使用 Admob,当我在 RewardedAdLoadCallback 的 onAdFailedToLoad() 下打印 loadAdError.message 时,我在日志中收到“网络错误”消息。

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.无论我使用移动数据还是 wifi,上面的 hasInternetAccess() 都会返回 true,并且 urlc.responseCode 是 204。我认为我的 wifi 没有问题,因为我可以浏览 web 并玩需要互联网的游戏,就可以了。

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. minSdk 为 23,我测试的 android 设备的版本为 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这是 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.我的 Android 应用程序多年来一直受到这个问题的困扰,并且在 2022 年似乎变得更糟。我希望他们能尽快解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase 在 Android Studio 模拟器上不起作用 - Firebase doesn't work on Android Studio Emulator Facebook 和 Google 登录不适用于 Android AAB 构建上传到 Firebase 应用程序分发 - Facebook and Google login doesn't work for Android AAB build uploaded to Firebase App Distribution iOS 推送通知不适用于 Firebase - iOS Push Notifications doesn't work with Firebase firebase-js-sdk v9 不适用于本机反应? 错误:尝试解析模块“idb”时 - firebase-js-sdk v9 doesn't work with react native? Error: While trying to resolve module `idb` Android 12 firebase 当应用程序在后台时,推送通知不起作用 - Android 12 firebase push notifications don't work when app is in the background Google App Engine 不适用于安全帽 - Google App Engine doesn't work with Hardhat list() 在 Google App Engine 中不起作用? - list() doesn't work in Google App Engine? Flutter / Firebase - 我的 iOS 设备在 WiFi 存储时无法连接到 ZBF12E1515C25C7D8C0352F14133AB9 - Flutter / Firebase - My iOS device will not connect to firebase / firestore while on WiFi 背景上的 Firebase 通知不适用于 Android - Firebase Notification onBackground don't work for Android Firebase 存储 listAll() 和 getDownloadURL() 不起作用 - Firebase Storage listAll() together with getDownloadURL() doesn't work out
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM