0

I want my application to give a notification when wifi goes 'offline'.

I figured out the notification part. But I can't seem to figure out the 'wifi check'-part. I read something about BroadcastReceiver but I can't seem to get it working. Any useful links? or example code? Tutorials?

Thanks in advance!

Updated with code. Its working but I need it to only give a notification when wifi goes offline.

    public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {        

    super.onCreate(savedInstanceState);  
    this.registerReceiver(this.mConnReceiver,
            new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
    setContentView(R.layout.activity_main);
}

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()){

        }else{
            showNotification();
        }
    }
};
E-Riz
  • 31,431
  • 9
  • 97
  • 134
user2883477
  • 153
  • 1
  • 3
  • 16
  • 1
    This is a duplicate of [broadcastreceiver-when-wifi-or-3g-network-state-changed](http://stackoverflow.com/questions/10733121/broadcastreceiver-when-wifi-or-3g-network-state-changed) – Steve Oct 15 '13 at 17:53
  • I didn't see that question. Anyhow, I updated my question, it's different now I guess haha. – user2883477 Oct 15 '13 at 18:11
  • Then you need to start a new thread. – Steve Oct 15 '13 at 18:15

1 Answers1

2

You can try something like this

In Manifest.xml
  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

In your application Code:

public class BroadCastSampleActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    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();
        }
    }
};
}
Bhanu Kaushik
  • 876
  • 6
  • 25
  • Thanks! I only think this is specified for network connectivity. I need it to only react to the wifi connectivity. Any advice? – user2883477 Oct 15 '13 at 17:45
  • the code will notify when the wifi connection is setup. I think that was your original question – Bhanu Kaushik Oct 15 '13 at 17:46
  • It's not working correctly. It gives a Notification on start-up, and when wifi connection changes to ON on or OFF. I'll add my code in the startpost. I need to only give a notification when the wifi goes off. – user2883477 Oct 15 '13 at 18:03