我已经用 webview 创建了应用程序。如果我执行了任何操作并且网络已断开,我想显示一个警报。我尝试过以下方法,
在 oncreate 方法中添加了这个。
public class AndroidNetTestActivity extends Activity {
public static WebView webview;
private Handler mHandler = new Handler();
private boolean isConnected = true;
final String offlineMessageHtml = "Net is disconnected";
final String timeoutMessageHtml = "Connection timed out";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview=(WebView)findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("file:///android_asset/www/index.htm");
webview.addJavascriptInterface(new MyJavaScriptInterface(), "Android");
isConnected=isNetworkAvailable();
webview.setNetworkAvailable(isConnected);
webview.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
isConnected=isNetworkAvailable();
if (isConnected) {
// return false to let the WebView handle the URL
return false;
} else {
// show the proper "not connected" message
view.loadData(offlineMessageHtml, "text/html", "utf-8");
// return true if the host application wants to leave the current
// WebView and handle the url itself
return true;
}
}
@Override
public void onReceivedError (WebView view, int errorCode,
String description, String failingUrl) {
if (errorCode == ERROR_TIMEOUT) {
view.stopLoading(); // may not be needed
view.loadData(timeoutMessageHtml, "text/html", "utf-8");
}
}
});
webview.setWebChromeClient(new WebChromeClient());
}
final class MyJavaScriptInterface
{
public void ProcessJavaScript(final String scriptname, final String args)
{
mHandler.post(new Runnable()
{
public void run()
{
//ToDo
}
});
}
}
public boolean isNetworkAvailable() {
Context context = getApplicationContext();
ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null) {
//boitealerte(this.getString(R.string.alert),"getSystemService rend null");
} else {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}
}
如果我点击登录按钮,如果网络不可用,它应该显示一条错误消息。
但它不起作用。请检查我的代码并告诉我我做错了什么
你可以这样检查互联网连接
boolean check=checkConnection();
if(check==true){
Toast.makeText(
this,
"Internet is Connected",
Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(
this,
"Failed to connect to internet.",
Toast.LENGTH_LONG).show();
}
and here is a method of checkConnection
protected boolean checkConnection(){
ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = conMan.getActiveNetworkInfo();
final boolean connected = networkInfo != null
&& networkInfo.isAvailable()
&& networkInfo.isConnected();
if ( !connected) {
Toast.makeText(
this,
"Failed to connect to internet.",
Toast.LENGTH_LONG).show();
return false;
}
return true;
}
就像这样做它对我有用
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)