Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First of all You need to create handler above on click method</p> <pre><code>private ProgressDialog pd; Thread thread = null; private Handler handler = new Handler() { public void handleMessage(Message msg) { pd.dismiss(); if (msg.what == 11) { Your code (for ex Toasting Message or passing intents or displaying somthing....) } else if (msg.what == 0) { } else { showNetworkAlert(); } } }; </code></pre> <p>then Creating progress Dialog in onCreate method </p> <pre><code> if (isOnline(Your class.this)) { pd = ProgressDialog.show(Your Class.this, "", "Loading...", true, false); new Thread(new Runnable() { @Override public void run() { try { Calling your parsing method .... handler.sendEmptyMessage(11); } catch (Exception e) { System.out.println("In Cache :"); handler.sendEmptyMessage(1); } } }).start(); } else { showNetworkAlert(); } </code></pre> <p>Create this method for Checking internet </p> <pre><code>public static Boolean isOnline(Context context) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo netInfo = cm.getActiveNetworkInfo(); if (netInfo != null &amp;&amp; netInfo.isConnectedOrConnecting()) { return true; } return false; } </code></pre> <p>Displaying alert if Internet is not Present </p> <pre><code>public void showNetworkAlert() { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setMessage("Please check your internet connection.") .setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); } }); builder.setTitle("Connection Problem"); builder.show(); } </code></pre>
 

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