我想创建一个应用程序。如果数据连接已打开,则自动显示 toast。否则不显示。我创建了很多应用程序,但我不知道如何制作。我试试这个但不工作。
公共类 MainActivity 扩展 Activity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webView = (WebView) findViewById(R.id.wv);
if(CheckNetwork.isInternetAvailable(MainActivity.this))
{
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
// webView = (WebView) findViewById(R.id.wv);
// webView.setWebViewClient(new MyWebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
// Activities and WebViews measure progress with different scales.
// The progress meter will automatically disappear when we reach 100%
MainActivity.this.setProgress(progress * 1000);
}
});
webView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(MainActivity.this, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
});
webView.loadUrl("http://slashdot.org/");
}
else{
//no connection
Toast toast = Toast.makeText(MainActivity.this, "No Internet Connection", Toast.LENGTH_LONG);
toast.show();
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
}
else
{
finish();
}
return super.onKeyDown(keyCode, event);
}
}