0

I built an app that streams audio using a webview. Now I added a check if there's connectivity (or not). Using toast I show a message with "true" or "false", but I'd like show a toast only when the connectivity changes. What should I do?

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

update();

private void update() {
        new Thread() {
            public void run() {
                while (true) {
                    runOnUiThread(new Runnable() {
                         @Override
                         public void run() {
                             isOnline();
                         }
                    });
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } 
            }
        }.start();
        }


    public boolean isOnline() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnectedOrConnecting()) {

            Toast.makeText(getApplicationContext(), "true",Toast.LENGTH_LONG).show();   
            return true;
        }
        Toast.makeText(getApplicationContext(), "false",Toast.LENGTH_LONG).show();
        return false;
thegrinner
  • 11,546
  • 5
  • 41
  • 64
Pol Hallen
  • 1,852
  • 6
  • 32
  • 44
  • 1
    Are you wanting to listen for a change in connectivity and when that connectivity changes, put up a Toast to inform you of the new connectivity state? – SnowInferno Jan 25 '13 at 18:59
  • yep exact! I don't know how do this. thanks – Pol Hallen Jan 25 '13 at 19:00
  • I don't have the knowledge to answer your question but maybe this will be helpful: http://stackoverflow.com/questions/1560788/how-to-check-internet-access-on-android-inetaddress-never-timeouts – SnowInferno Jan 25 '13 at 19:12

3 Answers3

2

Try this :

public class BroadCastSampleActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            this.registerReceiver(this.mConnReceiver,
                    new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
        }
        private BroadcastReceiver mConnReceiver = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
                String reason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON);
                boolean isFailover = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false);

                NetworkInfo currentNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
                NetworkInfo otherNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);

                if(currentNetworkInfo.isConnected()){
                    Toast.makeText(getApplicationContext(), "Connected", Toast.LENGTH_LONG).show();
                }else{
                    Toast.makeText(getApplicationContext(), "Not Connected", Toast.LENGTH_LONG).show();
                }
            }
        };
}

Add this permission in your Manifest :

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Pratik Sharma
  • 13,307
  • 5
  • 27
  • 37
1

The ConnectivityManager broadcasts the CONNECTIVITY_ACTION ("android.net.conn.CONNECTIVITY_CHANGE") action whenever the connectivity details have changed. You can register a broadcast receiver in your manifest to listen for these changes and resume (or suspend) your background updates accordingly.

That comes from the android developer information.

You can read more here: http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html

That should have everything you need to show the toast notifications.

Bmoore
  • 315
  • 4
  • 16
0

You should register for android.net.conn.CONNECTIVITY_CHANGE. And show a dialog only on receiving the intent.

You can create an inner class that extends a BroadcastReceiver and shows a toast in it's onReceive method.

That should work :)

Aman Gautam
  • 3,549
  • 2
  • 21
  • 25