基本上我的Android应用加载webview上的链接,如果没有互联网连接,我不想显示,所以我想要强制关闭应用程序或隐藏用户的链接。以下是我的代码
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class AndroidMobileAppSampleActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView mainWebView = (WebView) findViewById(R.id.mainWebView);
WebSettings webSettings = mainWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mainWebView.setWebViewClient(new MyCustomWebViewClient());
mainWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
mainWebView.loadUrl("my link which should not be displayed to user");
}
private class MyCustomWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}`enter code here`
我已经尝试过连接管理器,finish(),System.out.exit(0)和其他功能,但它并没有杀死应用程序。所以任何帮助都会很棒。
答案 0 :(得分:1)
试试这个:
private class MyCustomWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
AndroidMobileAppSampleActivity.this.finish();
}
}
答案 1 :(得分:0)
我得到了一个答案,只需输入代码
即可mWebView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
mWebView.loadUrl("file:///android_asset/myerrorpage.html");
}
});`
` 这将负责隐藏用户的URL