Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To check Network connection availability use the below</p> <pre><code> public class CheckNetwork { private static final String TAG = CheckNetwork.class.getSimpleName(); public static boolean isInternetAvailable(Context context) { NetworkInfo info = (NetworkInfo) ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo(); if (info == null) { Log.d(TAG,"no internet connection"); return false; } else { if(info.isConnected()) { Log.d(TAG," internet connection available..."); return true; } else { Log.d(TAG," internet connection"); return true; } } } } </code></pre> <p>In your Activity</p> <pre><code> if(CheckNetwork.isInternetAvailable(Activity.this)) //returns true if internet available { //do something. loadwebview. } else { Toast.makeText(Activity.this,"No Internet Connection",1000).show(); } </code></pre> <p>On back button press </p> <pre><code>@Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(event.getAction() == KeyEvent.ACTION_DOWN){ switch(keyCode) { case KeyEvent.KEYCODE_BACK: if(mWebView.canGoBack() == true){ mWebView.goBack(); }else{ finish(); } return true; } } return super.onKeyDown(keyCode, event); } </code></pre> <p>Update : if you need to check network connection availability at all times, System broadcasts events when there is connectivity change or loss of connectivity. You can capture those broadcasts in your activity by registering for the broadcast. But you need to unregister when not required.</p> <p>An example is at the below Link.</p> <p><a href="https://stackoverflow.com/questions/3141807/android-service-to-check-internet-connectivity">Android service to check internet connectivity?</a>.</p> <p>Or you can use a service that checks internet connectivity in the background. But you need to destroy the service when not required. But service is used for running long running operations in background</p> <p>Update 2:</p> <p>Try the below. I have tested. It works on my Samsung Galaxy S3.</p> <p>Add these permissions in manifest</p> <pre><code> &lt;uses-permission android:name="android.permission.INTERNET"/&gt; &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/&gt; &lt;uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/&gt; </code></pre> <p>Your activity_main.xml</p> <pre><code> &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" &gt; &lt;WebView android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:id="@+id/wv"/&gt; &lt;/RelativeLayout&gt; </code></pre> <p>Your activity</p> <pre><code> public class MainActivity extends Activity { private WebView webView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_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) &amp;&amp; webView.canGoBack()) { webView.goBack(); return true; } else { finish(); } return super.onKeyDown(keyCode, event); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload