Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
367 views
in Technique[技术] by (71.8m points)

android - Broadcast Receiver and Internet connection

How do I use broadcast receiver to check if there isn't Internet connection?

And after that:

1.If there is connection = do nothing

2.There isn't connection = open new activity

I hope you understand what I'm asking about.

Here is my webview app code:

WebView mWebView;
    String URL = "http://url.com";
    ProgressBar loadingProgressBar,loadingTitle;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);


        mWebView = (WebView) findViewById(R.id.Web);
        mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setBuiltInZoomControls(true);
        mWebView.getSettings().setAllowFileAccess(true);
        mWebView.loadUrl(URL);
        mWebView.setWebViewClient(new MyWebViewClient());

        loadingProgressBar=(ProgressBar)findViewById(R.id.progressBar); 

        mWebView.setWebChromeClient(new WebChromeClient() {

        @Override
        public void onProgressChanged(WebView view, int newProgress) {
        super.onProgressChanged(view, newProgress);
        loadingProgressBar.setProgress(newProgress);
        if (newProgress == 100) {
        loadingProgressBar.setVisibility(View.GONE);
        } else{
        loadingProgressBar.setVisibility(View.VISIBLE);
        }
        }
        });
    }



    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


        private class MyWebViewClient extends WebViewClient {


      @Override
      public boolean shouldOverrideUrlLoading(WebView view, String url) {
          if (url.endsWith(".mp3")) {
              Intent intent = new Intent(Intent.ACTION_VIEW);
              intent.setDataAndType(Uri.parse(url), "audio/*");
              view.getContext().startActivity(intent);   
              return true;
          } else if (url.endsWith(".mp4") || url.endsWith(".3gp")) {
                  Intent intent = new Intent(Intent.ACTION_VIEW); 
                  intent.setDataAndType(Uri.parse(url), "video/*");
                  view.getContext().startActivity(intent);   
                  return true;
          } else {
              return super.shouldOverrideUrlLoading(view, url);
          }
        }
      }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
  case R.id.menu_paivita:
    mWebView.reload();
    return true;
  case R.id.menu_sulje:
    a.this.finish();
    return true;
  case R.id.menu_tietoa:
          Intent intent = new Intent(a.this, c.class);
          intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          startActivity(intent);
         return true;
  case R.id.menu_palautetta:
      Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
      String aEmailList[] = { "[email protected]" };
      emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, aEmailList);
      emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Palaute Sovelluksesta");
      emailIntent.setType("plain/text");
      startActivity(Intent.createChooser(emailIntent, "Send your email in: Gmail"));
      return true;
    default:
        return false;
}
}
private BroadcastReceiver mConnReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
        String reason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON);
                    boolean isFailover = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false);

        NetworkInfo currentNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
        NetworkInfo otherNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);

        // do application-specific task(s) based on the current network state, such
        // as enabling queuing of HTTP requests when currentNetworkInfo is connected etc.
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = cm.getActiveNetworkInfo();
        if (info != null) {
            if (!info.isConnected()) {
            }
        }
        else {
            setContentView(R.layout.connection_error);
        }
    }
};

Thanks for your help!

UPDATE: Code is updated!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

ConnectivityManager provides calls by which you can check for internet connectivity.

If you want to listen for a change in connectivity, create a listener that reacts to ConnectivityManager.CONNECTIVITY_ACTION


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...