检查互联网连接

时间:2011-08-23 14:35:03

标签: android

  

可能重复:
  How to determine Android internet connection?

我打电话给webview。我想实现,如果没有Internet连接,它不应该打开活动并在Toast中显示消息。请指南 呼叫点

public void onItemClick(AdapterView<?> parent, View v, int position, long id)  {
                 switch (position) {
                 case 0:     
                    Intent intent = new Intent(v.getContext(),Settings.class);
                      startActivity(intent);
                 break;

                 case 1:     
                    Intent intent1 = new Intent(v.getContext(),Web_services.class);
                      startActivity(intent1);
                 break;

和webview就像这样

public class News extends menu {
    private WebView webview;
    private static final String TAG = "Main";
    private ProgressDialog progressBar;  
      Prefs myprefs = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.weblayout);
        this.myprefs = new Prefs(getApplicationContext());
        this.webview = (WebView)findViewById(R.id.MyWebview);

        WebSettings settings = webview.getSettings();
        settings.setJavaScriptEnabled(true);
        webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        webview.setBackgroundColor(Color.parseColor("#000000"));
        webview.setVerticalScrollBarEnabled(false);
        webview.setHorizontalScrollBarEnabled(false);
        final AlertDialog alertDialog = new AlertDialog.Builder(this).create();

        progressBar = ProgressDialog.show(News .this, "Please Wait", "Loading...");
        this.myprefs = new Prefs(getApplicationContext());
        webview.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Log.i(TAG, "Processing webview url click...");
                view.loadUrl(url);
                return true;
            }

            public void onPageFinished(WebView view, String url) {
                Log.i(TAG, "Finished loading URL: " +url);
                if (progressBar.isShowing()) {
                    progressBar.dismiss();
                }
            }

            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Log.e(TAG, "Error: " + description);
                Toast.makeText(News.this, "Oh no! " + description, Toast.LENGTH_SHORT).show();
                alertDialog.setTitle("Error");
                alertDialog.setMessage(description);
                alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        return;
                    }
                });
                alertDialog.show();
            }
        });
        webview.loadUrl("");
    }

  }

0 个答案:

没有答案