如何设计"没有互联网连接页"适用于Android应用

时间:2016-04-04 05:03:46

标签: android

我想创建一个"没有互联网连接"当我的webapp没有连接到互联网并且重试"按钮应该在那里。我已经创建了一个error.html页面并放入了资产文件夹..

@SuppressLint("SetJavaScriptEnabled") 
public class MainActivity extends Activity {

//private Button button;
private WebView webView;
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Get webview 
    webView = (WebView) findViewById(R.id.mywebview);

    startWebView("http://abc/ashwini/my.html");

}

private void startWebView(String url) {

    //Create new webview Client to show progress dialog
    //When opening a url or click on link

    webView.setWebViewClient(new WebViewClient() {      
        ProgressDialog progressDialog;


        //If you will not use this method url links are opeen in new brower not in webview
        public boolean shouldOverrideUrlLoading(WebView view, String url) {              
            view.loadUrl(url);
            return true;

        }

        //Show loader on url load
        public void onLoadResource (WebView view, String url) {
            if (progressDialog == null) {
                // in standard case YourActivity.this
                progressDialog = new ProgressDialog(MainActivity.this);
                progressDialog.setMessage("Loading...");
                progressDialog.show();
            }
        }
        public void onPageFinished(WebView view, String url) {
            try{
            if (progressDialog.isShowing()) {
                progressDialog.dismiss();
                progressDialog = null;

            }
            }catch(Exception exception){
                exception.printStackTrace();
            }
        }

    }); 

     // Javascript inabled on webview  
    webView.getSettings().setJavaScriptEnabled(true); 

    // Other webview options
    /*
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setUseWideViewPort(true);
    webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    webView.setScrollbarFadingEnabled(false);
    webView.getSettings().setBuiltInZoomControls(true);
    */

    /*
     String summary = "<html><body>You scored <b>192</b> points.</body></html>";
     webview.loadData(summary, "text/html", null); 
     */

    //Load url in webview
    webView.loadUrl(url);


}

// Open previous opened link from history on webview when back button pressed

@Override
// Detect when the back button is pressed
public void onBackPressed() {
    if(webView.canGoBack()) {
        webView.goBack();
    } else {
        // Let the system handle the back button
        super.onBackPressed();
    }
}

}

2 个答案:

答案 0 :(得分:0)

   public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Get webview 
    webView = (WebView) findViewById(R.id.mywebview);

    if(isConnectionAvailable(getApplicationContext()))
    {
    startWebView("http://abc/ashwini/nointernet.html");
    }
    else
    {
    startWebView("http://abc/ashwini/my.html");
    }

}


  public static boolean isConnectionAvailable(Context context) {

    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivityManager != null) {
        NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnected()
                && netInfo.isConnectedOrConnecting()
                && netInfo.isAvailable()) {
            return true;
        }
    }
    return false;
}

许可

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

答案 1 :(得分:0)

这一个:

ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
  if(connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED ||connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) {
                            //we are connected to a network
                            // write your code by which you use to internet
  }
  else{
       final Dialog dialog = new Dialog(your activity name.this);
       //setting custom layout to dialog
       dialog.setContentView(R.layout.custom_dialog_layout);
       dialog.setTitle(" Oops... ");
       //adding text dynamically
       TextView txt = (TextView) dialog.findViewById(R.id.textView);
                            txt.setText( "Internet Connection is not Available");

      Button dismissButton = (Button) dialog.findViewById(R.id.button);
                            dismissButton.setText("Retry");
      dismissButton.setOnClickListener(new View.OnClickListener() {
               @Override
                public void onClick(View v) {
                          yourActivityname.this.finish();
                          startActivity(yourActivityname.this.getIntent());
                          dialog.dismiss();
                }
      });
     dialog.show();
 }