[英]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.