我已经为此工作了几天。我刚开始使用Android。我有一个两部分的问题。我想检查互联网是否可用。如果它是启动 webView 如果不发出警报并阻止 webView 加载。我已经尝试过了,但如果没有互联网连接(例如飞行模式),它会强制关闭。我如何让它发出消息并停止加载 webview。还有一种方法可以持续检查连接,以便在失去连接时不显示网页 404?
这是我的代码。
public class MainActivity extends Activity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
android.net.NetworkInfo wifi = cm
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo datac = cm
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if ((wifi != null & datac != null)
&& (wifi.isConnected() | datac.isConnected())) {
setContentView(R.layout.activity_main);
}else{
//no connection
Toast toast = Toast.makeText(MainActivity.this, "No Internet Connection", Toast.LENGTH_LONG);
toast.show();
}
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
webView = (WebView) findViewById(R.id.webview);
webView.setWebViewClient(new MyWebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://mysite.com/?ma=1");
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("mysite.com")) {
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.logout:
finish();
return true;
}
return false;
}
}
编辑:添加错误具体来说,在显示 Toast 说没有互联网连接后按回时出现这些错误。如果我在吐司消息后使用后退按钮,它会崩溃,并且错误如下。
04-07 14:40:38.664: E/AndroidRuntime(15173): FATAL EXCEPTION: main
04-07 14:40:38.664: E/AndroidRuntime(15173): java.lang.NullPointerException
04-07 14:40:38.664: E/AndroidRuntime(15173): at com.mysite.testapp.MainActivity.onKeyDown(MainActivity.java:67)
04-07 14:40:38.664: E/AndroidRuntime(15173): at android.view.KeyEvent.dispatch(KeyEvent.java:2705)
04-07 14:40:38.664: E/AndroidRuntime(15173): at android.app.Activity.dispatchKeyEvent(Activity.java:2401)
04-07 14:40:38.664: E/AndroidRuntime(15173): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2007)
04-07 14:40:38.664: E/AndroidRuntime(15173): at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3813)
04-07 14:40:38.664: E/AndroidRuntime(15173): at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:3761)
04-07 14:40:38.664: E/AndroidRuntime(15173): at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2926)
04-07 14:40:38.664: E/AndroidRuntime(15173): at android.os.Handler.dispatchMessage(Handler.java:99)
04-07 14:40:38.664: E/AndroidRuntime(15173): at android.os.Looper.loop(Looper.java:137)
04-07 14:40:38.664: E/AndroidRuntime(15173): at android.app.ActivityThread.main(ActivityThread.java:4918)
04-07 14:40:38.664: E/AndroidRuntime(15173): at java.lang.reflect.Method.invokeNative(Native Method)
04-07 14:40:38.664: E/AndroidRuntime(15173): at java.lang.reflect.Method.invoke(Method.java:511)
04-07 14:40:38.664: E/AndroidRuntime(15173): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
04-07 14:40:38.664: E/AndroidRuntime(15173): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
04-07 14:40:38.664: E/AndroidRuntime(15173): at dalvik.system.NativeStart.main(Native Method)