广播接收器抛出多个广播

时间:2018-07-12 18:05:14

标签: android broadcastreceiver

好。我很好奇它是否是我的代码,或者广播接收器是否正在执行应有的功能。我设置了一个广播接收器来侦听wifi的变化,例如是否连接了某个网络。广播接收器正常工作,并且在“接收”方法中将其信息发布到logcat中。这里的问题是它似乎多次触发,在本例中是8次。这是应该做的吗?有没有办法限制触发次数。

这是原木猫

07-12 13:33:21.748 8152-8152/com.apklegion.pcnotetest D/TEMP: 
android.net.wifi.STATE_CHANGE
07-12 13:33:21.753 8152-8152/com.apklegion.pcnotetest D/TEMP: 
SSID :: "SkyNet Global Defense Network "
07-12 13:33:21.754 8152-8152/com.apklegion.pcnotetest D/TEMP: Wifi is enabled
07-12 13:33:21.806 8152-8152/com.apklegion.pcnotetest D/TEMP: 
android.net.wifi.STATE_CHANGE
07-12 13:33:21.809 8152-8152/com.apklegion.pcnotetest D/TEMP: BSSID :: 
4c:60:de:cf:e3:88SSID :: "SkyNet Global Defense Network "
07-12 13:33:21.810 8152-8152/com.apklegion.pcnotetest D/TEMP: Wifi is enabled
07-12 13:33:21.862 8152-8152/com.apklegion.pcnotetest D/TEMP: 
android.net.wifi.STATE_CHANGE
07-12 13:33:21.865 8152-8152/com.apklegion.pcnotetest D/TEMP: SSID :: "SkyNet 
Global Defense Network "
07-12 13:33:21.866 8152-8152/com.apklegion.pcnotetest D/TEMP: Wifi is enabled
07-12 13:33:21.918 8152-8152/com.apklegion.pcnotetest D/TEMP: 
android.net.wifi.STATE_CHANGE
07-12 13:33:21.920 8152-8152/com.apklegion.pcnotetest D/TEMP: SSID :: "SkyNet 
Global Defense Network "
07-12 13:33:21.921 8152-8152/com.apklegion.pcnotetest D/TEMP: Wifi is enabled
07-12 13:33:21.966 8152-8152/com.apklegion.pcnotetest D/TEMP: 
android.net.wifi.STATE_CHANGE
07-12 13:33:21.969 8152-8152/com.apklegion.pcnotetest D/TEMP: SSID :: "SkyNet 
Global Defense Network "
07-12 13:33:21.970 8152-8152/com.apklegion.pcnotetest D/TEMP: Wifi is enabled
07-12 13:33:22.013 8152-8152/com.apklegion.pcnotetest D/TEMP: 
android.net.wifi.STATE_CHANGE
07-12 13:33:22.017 8152-8152/com.apklegion.pcnotetest D/TEMP: SSID :: "SkyNet 
Global Defense Network "
07-12 13:33:22.018 8152-8152/com.apklegion.pcnotetest D/TEMP: Wifi is enabled
07-12 13:33:22.073 8152-8152/com.apklegion.pcnotetest D/TEMP: 
android.net.wifi.STATE_CHANGE
07-12 13:33:22.075 8152-8152/com.apklegion.pcnotetest D/TEMP: SSID :: "SkyNet 
Global Defense Network "
07-12 13:33:22.076 8152-8152/com.apklegion.pcnotetest D/TEMP: Wifi is enabled
 07-12 13:33:22.386 8152-8152/com.apklegion.pcnotetest D/TEMP: 
 android.net.wifi.STATE_CHANGE
07-12 13:33:22.391 8152-8152/com.apklegion.pcnotetest D/TEMP: SSID :: "SkyNet 
Global Defense Network "
07-12 13:33:22.392 8152-8152/com.apklegion.pcnotetest D/TEMP: Wifi is enabled
Wifi is  connected

这是广播接收器类

public class MyWifiStateListener extends BroadcastReceiver {

Boolean isConnected = false;
String skynet, message;
public static boolean isConn = false;

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


                            // TODO Auto-generated method stub
                            String action = intent.getAction();
                            Log.d("TEMP", action);


                            assert action != null;
                            if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {

                                NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);


                                if (info.getType() == ConnectivityManager.TYPE_WIFI) {
                                    WifiManager myWifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
                                    WifiInfo wifiInfo = myWifiManager.getConnectionInfo();
                                    String test = wifiInfo.toString();
                                    String sid = wifiInfo.getSSID();


                                    tv.setText(test);
                                    int count =0;

                                    Log.d("TEMP", "SSID :: " + wifiInfo.getSSID());

在我的“主要活动”课程中,我注册了接收者,并在OnPause和简历中设置了取消注册和重新注册的方法

   filters.addAction("android.net.wifi.STATE_CHANGE");
    super.registerReceiver(myWifiStateListener, filters);

@Override
protected void onPause() {
   try {
       unregisterReceiver(myWifiStateListener);
       Log.i(TAG, "Unregistered broadcast receiver");
   }catch (Exception e){

    }
    super.onPause();
}


@Override
protected void onResume() {
    Log.i(TAG, "Registered broadcast receiver");
    registerReceiver(myWifiStateListener,filters);
    super.onResume();
}


@Override
protected void onDestroy() {
   stopService(new Intent(this, MyWifiStateListener.class));
    Log.i(TAG, "Stopped service");
    super.onDestroy();
}

0 个答案:

没有答案