[英]Replace a “Web page not available” Error in WebView with something less ugly? (Android)
[英]How to Replace Webpage Not Available Page To Custom Page? (WEBVIEW)
我想從Webpage not Available
或ERR_NAME_NOT_RESOLVED
更改頁面
到我的頁面“無互聯網連接” ?
並帶有刷新按鈕,如果在線或再次連接互聯網,則可以重新上線,如果沒有連接,請留在 Page No Internet Connection 。
看起來像一個頁面HTML
不是默認的通知頁面android
。
我正在使用帶有Kotlin
的Android Studio
這是MyActivity
class MainActivity : AppCompatActivity() {
internal var webView: WebView? = null
private var mCM: String? = null
private var mUM: ValueCallback<Uri>? = null
private var mUMA: ValueCallback<Array<Uri?>>? = null
//select whether you want to upload multiple files (set 'true' for yes)
private val multiple_files = false
@SuppressLint("SetJavaScriptEnabled", "WrongViewCast")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
webView = findViewById(R.id.ifView) as WebView
assert(webView != null)
val webSettings = webView!!.settings
webSettings.javaScriptEnabled = true
webSettings.allowFileAccess = true
if (Build.VERSION.SDK_INT >= 21) {
webSettings.mixedContentMode = 0
webView!!.setLayerType(View.LAYER_TYPE_HARDWARE, null)
}
else {
webView!!.setLayerType(View.LAYER_TYPE_SOFTWARE, null)
}
webView!!.webViewClient = Callback()
webView!!.loadUrl("http://xxx.xxx.xxx") //add your test web/page address here
webView!!.setWebChromeClient(object : WebChromeClient()
}
這是我的代碼Callback
inner class Callback : WebViewClient() {
override fun onReceivedError(view: WebView, errorCode: Int, description: String, failingUrl: String) {
Toast.makeText(applicationContext, "Failed loading app!", Toast.LENGTH_SHORT).show()
}
}
當然,我的應用程序希望在webview
離線時加載離線頁面以通知應用程序未連接互聯網,並在webview
在線時再次連接到互聯網顯示在線頁面
用您的自定義 CustomWebviewClient 替換默認的 WebChromeClient,如下所示
webView!!.setWebViewClient(object : CustomWebviewClient()
然后創建這個自定義 WebViewClient
inner class CustomWebviewClient : WebViewClient() {
override fun onPageStarted(view: WebView, url: String, favicon: Bitmap) {
super.onPageStarted(view, url, favicon)
}
override fun onPageFinished(view: WebView, url: String) {
super.onPageFinished(view, url)
}
override fun onReceivedError(view: WebView, request: WebResourceRequest, error: WebResourceError) {
super.onReceivedError(view, request, error)
if (noInternetConnection()) {
loadNoInternetErrorView()
} else {
loadErrorMessageView()
}
}
}
每當您的 WebView 無法加載頁面時,它就會調用onReceivedError
回調,您可以在那里顯示錯誤布局。 對於錯誤視圖,您可以使用初始可見性 View.GONE 在布局中創建錯誤布局,並在發生錯誤時使其可見。
對XML文件的更改
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:id="@+id/error_screen"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Textview
android:id="@+id/error_msg" />
................
<ImageView
android:id=:"@+id/error_icon/>
//error view
</LinearLayout>
在loadErrorMessageView你可以這樣做
loadErrorMessageView() {
webview.setVisibility(View.GONE);
error_screen.setVisibility(View.VISIBLE);
error_msg.setText("Some error Occurred");
}
同樣,你可以為 NoInternet 案例做也許你可以顯示“沒有互聯網消息”而不是“發生了一些錯誤”我希望這能消除你的疑慮
我想從Webpage not Available
或ERR_NAME_NOT_RESOLVED
更改頁面
到我的頁面“沒有Internet連接” ?
並帶有一個刷新按鈕,可以重新聯機(如果已聯機或已連接互聯網),如果沒有連接,請停留在Page No Internet Connection中。
看起來像頁面HTML
不是默認的notif頁面android
或此調用(如Custom Page) 。
我在Kotlin
使用Android Studio
這是MyActivity
class MainActivity : AppCompatActivity() {
internal var webView: WebView? = null
private var mCM: String? = null
private var mUM: ValueCallback<Uri>? = null
private var mUMA: ValueCallback<Array<Uri?>>? = null
//select whether you want to upload multiple files (set 'true' for yes)
private val multiple_files = false
@SuppressLint("SetJavaScriptEnabled", "WrongViewCast")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
webView = findViewById(R.id.ifView) as WebView
assert(webView != null)
val webSettings = webView!!.settings
webSettings.javaScriptEnabled = true
webSettings.allowFileAccess = true
if (Build.VERSION.SDK_INT >= 21) {
webSettings.mixedContentMode = 0
webView!!.setLayerType(View.LAYER_TYPE_HARDWARE, null)
}
else {
webView!!.setLayerType(View.LAYER_TYPE_SOFTWARE, null)
}
webView!!.webViewClient = Callback()
webView!!.loadUrl("http://xxx.xxx.xxx") //add your test web/page address here
webView!!.setWebChromeClient(object : WebChromeClient()
}
這是我的代碼Callback
inner class Callback : WebViewClient() {
override fun onReceivedError(view: WebView, errorCode: Int, description: String, failingUrl: String) {
Toast.makeText(applicationContext, "Failed loading app!", Toast.LENGTH_SHORT).show()
}
}
可以肯定的是,我的應用程序希望加載脫機頁面,如果webview
程序未連接互聯網,則將webview
脫機,並在webview
聯機時再次連接到顯示在線頁面的互聯網
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.