[:en]
Step 1: AndroidManifest.xml
Step 2: MainActivity.java
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://cambridgeways.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); }
Step 3: activity_main.xml
Or Simple Testing
public class MainActivity extends AppCompatActivity { private WebView webView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if(isConnectingToInternet(getApplicationContext())) { Toast.makeText(getApplicationContext(),"Internet available",Toast.LENGTH_LONG).show(); }else{ 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; } }
[:]