public class MainActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);TextView wifistate = (TextView) findViewById(R.id.infowifi);}this.registerReceiver(mWifiStateChangedReceiver,new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION));
@Override public void onBackPressed() { unregisterReceiver(mWifiStateChangedReceiver); // this will unregister Receiver this.finish(); // this will finish ThisActivity super.onBackPressed(); }private BroadcastReceiver mWifiStateChangedReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub int extraWifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN); doit(); switch (extraWifiState) { case WifiManager.WIFI_STATE_DISABLED: // wifi is disabled wifistate.setText("OFF"); // do whatever you want here break; case WifiManager.WIFI_STATE_DISABLING: // wifi is in process of disabling wifistate.setText("OFF"); // do whatever you want here break; case WifiManager.WIFI_STATE_ENABLED: // wifi is enabled wifistate.setText("ON"); // do whatever you want here break; case WifiManager.WIFI_STATE_ENABLING: // wifi is in process of enabling wifistate.setText("ON"); // do whatever you want here break; case WifiManager.WIFI_STATE_UNKNOWN: // wifi state is unknown wifistate.setText("N/A"); // do whatever you want here break; } } };}
How to update WiFi state using BroadcastReceiver
This BroadcastReceiver will trigger anytime "Mobile data" disconnects or connects.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment