Issue
Depending on WiFi connection (whether it's "on" or "off"), I want to display one of the two views/activities: No Connection or WebView with URL. I'm using BroadcastReceiver
to check the connection.
Everything seems to work fine when the app loads, but when I toggle WiFi "off" and then "on" again (whenever I leave WebView and then come back), WebView URL doesn't load (get Webpage not available message). No Connection view works always, so I'm assuming there is something with WebView. And also, WebView works if I add 5 second delay and then do loadUrl
again. I can also see that WiFi status is detected properly.
I would appreciate any input. I've googled it, but all I can find is basic WebView examples. I'm very new to Android, so maybe I'm missing something basic. Thanks in advance for your help!
``` class WebViewActivity : AppCompatActivity() {
private lateinit var wifiManager : WifiManager
private lateinit var intentMain: Intent
private lateinit var webView: WebView
override fun onCreate(savedInstanceState: Bundle?) {
Log.d("WIFI_TEST", "WV: onCreate")
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_web_view)
wifiManager = applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
intentMain = Intent(this, MainActivity::class.java)
webView = findViewById(R.id.webview)
webView.settings.javaScriptEnabled = true
webview.settings.domStorageEnabled = true
webView.webViewClient = WebViewClient()
webView.loadUrl("https://google.com")
}
override fun onStart() {
super.onStart()
val intentFilter = IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION)
registerReceiver(wifiStateReceiver, intentFilter)
}
override fun onStop() {
super.onStop()
webView.destroy()
unregisterReceiver(wifiStateReceiver)
}
override fun onBackPressed() {
if (webView.canGoBack()) {
webView.goBack()
} else {
super.onBackPressed()
}
}
private val wifiStateReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val wifiStateExtra = intent.getIntExtra(
WifiManager.EXTRA_WIFI_STATE,
WifiManager.WIFI_STATE_UNKNOWN
)
when (wifiStateExtra) {
WifiManager.WIFI_STATE_DISABLED -> {
Log.d("WIFI_TEST", "WV: OFF")
startActivity(intentMain)
}
WifiManager.WIFI_STATE_ENABLED -> {
Log.d("WIFI_TEST", "WV: ON")
// Works with delay with at least 5 sec
// Why?
Timer().schedule(5000) {
Log.d("WIFI_TEST", "WV: delay")
webView.post(Runnable {
webView.loadUrl("https://google.com")
})
}
}
}
}
}
} ```
Solution
Remember that when the Wifi is enabled again probably is available but you have to wait some milliseconds for connectivity.
WifiManager.WIFI_STATE_ENABLED -> {
Log.d("WIFI_TEST", "WV: ON")
// Works with delay with at least 5 sec
// Why?
Timer().schedule(5000) {
Log.d("WIFI_TEST", "WV: delay")
webView.post(Runnable {
webView.loadUrl("https://google.com")
})
}
}
isAvailable(): Indicates whether network connectivity is possible. A network is unavailable when a persistent or semi-persistent condition prevents the possibility of connecting to that network.
isConnected(): Indicates whether network connectivity exists and it is possible to establish connections and pass data.
Answered By - Jorgesys
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.