Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use <code>runOnUiThread</code> and <code>ConnectivityManager</code> for checking Network Status after a fixed time and show message to user as:</p> <pre><code>private boolean mNetworkRunning=false; private ConnectivityManager connMgr; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView (R.layout.main); mClockRunning=true; NetworkStatusThread(); } @Override protected void onDestroy() { super.onDestroy(); // Stop Thread Here. mNetworkRunning=false; } public void NetworkStatusThread(){ Thread th=new Thread(){ @Override public void run(){ try { while(mNetworkRunning) { Thread.sleep(1000L);// set time here for refresh time in show update to user NetworkActivity.this.runOnUiThread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub if(mNetworkRunning) { boolean statu=isMobileNetworkAvailable(); if(statu==true) Toast.makeText(YourCurrentActivity.this, "Network Available",Toast.LENGTH_LONG).show(); else Toast.makeText(YourCurrentActivity.this, "Network Not Available",Toast.LENGTH_LONG).show(); } }; } }catch (InterruptedException e) { // TODO: handle exception } } }; th.start(); } public boolean isMobileNetworkAvailable(Context con){ if(null == connMgr){ connMgr = (ConnectivityManager)con.getSystemService(Context.CONNECTIVITY_SERVICE); } NetworkInfo wifiInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); NetworkInfo mobileInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if(wifiInfo.isAvailable()){ return true; }else if(mobileInfo.isAvailable()){ return true; }else{ return false; } } </code></pre> <p>and second option without using Thread then you can register an receiver for <code>CONNECTIVITY_CHANGE</code> Manifest.xml as:</p> <pre><code>&lt; !-- Needed to check when the network connection changes --&gt; &lt; uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/&gt; &lt; receiver android:name="you_package_name.ConnectionChangeReceiver" android:label="NetworkConnection"&gt; &lt; intent-filter&gt; &lt; action android:name="android.net.conn.CONNECTIVITY_CHANGE"/&gt; &lt; /intent-filter&gt; &lt; /receiver&gt; </code></pre> <p>in code:</p> <pre><code>public class ConnectionChangeReceiver extends BroadcastReceiver { private static final String TAG =ConnectionChangeReceiver.class.getSimpleName(); @Override public void onReceive(Context context, Intent intent) { boolean success = false; ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); State state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState(); //WIFI if (State.CONNECTED == state) { success = true; } //GPRS state = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState(); if (State.CONNECTED != state) { success = true; } if (!success) { Toast.makeText(context, "SHOW STATUS HERE", Toast.LENGTH_LONG).show(); } } </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.
 

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