Android detect wifi state changes

When your application relies on Wifi network connection to send and receive data, it is important to notify users of connection changes before they download or upload data from and to a remote server. To achieve this goal, you need have a BroadcastReiver that is registered to receive the Wifi state changes. When there is a change in Wifi connection, the System broadcasts and Intent object that contains a state of the Wifi connection. You can get the state by calling the getExtra() method of the Intent with passing WifiManager.EXTRA_WIFI_STATE as its key parameter. You will use the constants: WIFI_STATE_ENABLED, WIFI_STATE_ENABLING, WIFI_STATE_DISABLED, and WIFI_STATE_DISABLING to determine whether the Wifi is enable, enabling, disable, or disabling.



Now to have to have an example application on detecting Wifi state changes, you create a new Android project. Then in the MainActivity class, you create a BroadcastReceiver. In the onResume() method you register the receiver and in the onPause() method to unregister the receiver.

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WifiManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {

   private BroadcastReceiver WifiReceiver = new BroadcastReceiver() {
     @Override
     public void onReceive(Context context, Intent intent) {
        int WifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
             WifiManager.WIFI_STATE_UNKNOWN);

          switch (WifiState) {
          case WifiManager.WIFI_STATE_ENABLED: {
             Toast.makeText(context,"Wifi enabled",Toast.LENGTH_LONG).show();
             }
           break;

           case WifiManager.WIFI_STATE_ENABLING: {
                Toast.makeText(context,"Wifi enabling",Toast.LENGTH_LONG).show();
             }
           break;
           case WifiManager.WIFI_STATE_DISABLED: {
             Toast.makeText(context,"Wifi disabled",Toast.LENGTH_LONG).show();

           }
           break;

           case WifiManager.WIFI_STATE_DISABLING: {
           }
           break;



           case WifiManager.WIFI_STATE_UNKNOWN: {
           }
           break;

        }
     } };

   protected void onResume(){
     super.onResume();
     registerReceiver(WifiReceiver, new IntentFilter( WifiManager.WIFI_STATE_CHANGED_ACTION));
   }

   protected void onPause(){
     super.onPause();
     unregisterReceiver(WifiReceiver);
   }
}



Android-detect-wifi-state-changes

Posted by: Dara | post date: 03-09-2015 | Subject: Android Apps Development

write programming tip




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.