简体   繁体   中英

Android:The if statement of wifi scan result is not being entered?

I have built BroadcastReceiver in my MainActivity to catch system broadcast(internet connection and wifi scan result). The internet connection broadcast is being caught but I am facing problem to catch the broadcast of the wifi scan result. no error is being thrown. I do not know what shall I add additional to get it work. I appreciate any help.

MainActivity:

 public class MainActivity extends ActionBarActivity { BroadcastReceiverListener receiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); receiver = new BroadcastReceiverListener(); } private class BroadcastReceiverListener extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //This if statement is being arrived if (intent.getAction().equals( android.net.wifi.WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) { //This code works without BroadcastReceiver. } else if (intent.getAction().equals( android.net.ConnectivityManager.CONNECTIVITY_ACTION)) { // I am getting here broadcast for the internet connection } } }; protected void onResume() { IntentFilter wifi = new IntentFilter(); wifi.addAction(android.net.wifi.WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); registerReceiver(receiver, wifi); IntentFilter conn = new IntentFilter(); conn.addAction(android.net.ConnectivityManager.CONNECTIVITY_ACTION); registerReceiver(receiver, conn); super.onResume(); } @Override protected void onDestroy() { unregisterReceiver(receiver); super.onDestroy(); } } 

There's no need to create two IntentFilters, just create the one and use addAction() to add multiple actions:

IntentFilter wifi = new IntentFilter();
wifi.addAction(android.net.wifi.WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
wifi.addAction(android.net.ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(receiver, wifi);

See here:

Android - Registering a broadcast receiver for two intents?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM