简体   繁体   中英

Checking internet connection in android studio

I was working in android studio 1.3.2 for some days, I am very much new to android development. Here I need to check the internet connection, if internet is there I need to display a webpage, and if internet is not there, I need to display an alert box saying 'no internet connection found'. This is the code I have done so far.

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webView = (WebView) findViewById(R.id.activity_main);
        webView.setWebViewClient(new MyWebViewClient());
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

        String url = "http://facebook.com";
        webView.getSettings().setJavaScriptEnabled(true);
        if(isConnectingToInternet(getApplicationContext()))   {
            webView.loadUrl(url);
        }else{
            // show alert

            Toast.makeText(getApplicationContext(), "no internet", Toast.LENGTH_LONG).show();
        }
    }



    private boolean isConnectingToInternet(Context applicationContext){
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getActiveNetworkInfo();
        if (ni == null) {
            // There are no active networks.
            Toast.makeText(getApplicationContext(), "no internet", Toast.LENGTH_LONG).show();
            return false;
        } else
            return true;

    }


    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

But the alert is not showing when there is no internet connection.

I have enabled

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

in AndroidManifest.xml. I am stucked here, I don't know what to do next. I googled alot, but couldn't find an answer. Can anyone please help me?

把它放在你的Android清单中

 <uses-permission android:name="android.permission.INTERNET" />

check internet connect if connected load url in your webview else show alert.

EDIT: your onCreate() should look like this:

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webView = (WebView) findViewById(R.id.sufi_boutique);
        webView.setWebViewClient(new MyWebViewClient());
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

        String url = "http://facebook.com";
        webView.getSettings().setJavaScriptEnabled(true);
       if(isConnectingToInternet(getApplicationContext()))   {    
             webView.loadUrl(url);
       }else{
          // show alert
          Toast.makeText(getApplicationContext(),"no internet",Toast.LENGTH_LONG).show();
       }

    }

make sure you have added internet permission in your manifest file

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM