简体   繁体   中英

Android: one BroadcastReceiver for muliple broadcast

I want to have one BroacastReceiver for my application so I want all broadcast actions (wifiManger.getResultscan;connectivityManager.getActiveNetworkInfo; and maybe other ones ) to be detected in the BoradcastListener. How can I connect the BroadcastReceiver in the MainActivity with the broadcastListner calss?

  private void check_wifi_available() { WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); registerReceiver(new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); final List<ScanResult> results = wifiManager.getScanResults(); if (results != null) { // list of access points from the last scan List<ScanResult> updatedResults = new ArrayList<ScanResult>(); // pick Wi-Fi access points which begins with these "SV-" // characters. for (int i = 0; i < results.size(); i++) { String ssid = results.get(i).SSID; // Pattern p = Pattern.compile("^KD-(4[0-9]{2}|500)$"); // Matcher m = p.matcher(ssid); // if(m.matches()){}else{} if (ssid.startsWith("KD")) { updatedResults.add(results.get(i)); } } if (updatedResults.size() > 0) { String a = deliverBestAccessPoint(updatedResults); textWifi.setText(a.toString()); } } } }, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); 

  public void onReceive(Context context, Intent intent) { ConnectivityManager connectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo(); boolean isConnected = netInfo != null && netInfo.isConnectedOrConnecting(); if (isConnected) { Toast.makeText(context, "The device is connected to the internet ", Toast.LENGTH_SHORT).show(); Log.i("NET", "connecte" + isConnected); } else { Toast.makeText(context, "Please connect the device to the internet.", Toast.LENGTH_SHORT).show(); } } 

First Create a receiver variable

private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
     //Compare intent.getAction() with your required intents
}

Then call registerReceiver

registerReceiver(receiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));registerReceiver(receiver, new IntentFilter(other required intent filter constants));

Just create IntentFilter separately and add all actions you want to handle:

    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction("action1");
    intentFilter.addAction("action2");
    registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // I have this one here to dectect wifi access points (getResultScan)
        }, intentFilter);

Create a new class in your package name;

public class DiscoDancer extends BroadcastReceiver {

       @Override
       public void onReceive(Context context, Intent intent) {

      //check here with, intent.getAction() and macth with your action

       }
    }

In your AndroidManifest.xml

<receiver android:name="DiscoDancer">
      <intent-filter>
         <action android:name="android.intent.action.BOOT_COMPLETED">
         <action android:name="android.intent.action.ACTION_BATTERY_LOW">
    //Add all the intents here

      </action>
      </intent-filter>
</receiver>

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