Android BroadcastReceiver多次调用

时间:2016-09-05 09:41:45

标签: android broadcastreceiver android-broadcast android-broadcastreceiver hotspot

我是Android的新手,我正在尝试实现一个广播接收器,只要热点发生变化就会收到广播:

public class HotspotChangeReceiver extends BroadcastReceiver implements Configurable {

public static boolean isEnabled = false;


@Override
public void onReceive(Context context, Intent intent) {
    //TODO handle properly
    String action = intent.getAction();
    if ("android.net.wifi.WIFI_AP_STATE_CHANGED".equals(action)) {

        Intent intent1 = new Intent();
        int state = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);

       // if (WifiManager.WIFI_STATE_ENABLED == state % 10) {
         if(state == 13){
            intent1.setAction("net.wecodelicious.intent.action.HOTSPOT_ENABLED");
            context.sendBroadcast(intent1);
        }
        else{
            intent1.setAction("net.wecodelicious.intent.action.HOTSPOT_DISABLED");
            context.sendBroadcast(intent1);
        }

        Toast.makeText(context, "HOTSPOT Changed ", Toast.LENGTH_LONG).show();

    }
}

public static void enable() {
    if(!isEnabled) {
        MainActivity.getMpackageManager().setComponentEnabledSetting(new ComponentName(MainActivity.getMcontext(), HotspotChangeReceiver.class), PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
        isEnabled = true;
    }
}


public static void disable() {
    if(isEnabled) {
        MainActivity.getMpackageManager().setComponentEnabledSetting(new ComponentName(MainActivity.getMcontext(), HotspotChangeReceiver.class), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
        isEnabled = false;
    }
}
}

我也按照课程接收自定义广播:

public class HotspotEnabledReceiver extends BroadcastReceiver implements Configurable {
public static boolean isEnabled = false;

@Override
public void onReceive(Context context, Intent intent) {
    //TODO handle properly
    if("net.wecodelicious.intent.action.HOTSPOT_ENABLED".equals(intent.getAction())) {
        Toast.makeText(context, "Hotspot Enabled ", Toast.LENGTH_LONG).show();
    }
}

public static void enable() {
    if(!isEnabled) {
        HotspotChangeReceiver.enable();
        MainActivity.getMpackageManager().setComponentEnabledSetting(new ComponentName(MainActivity.getMcontext(), HotspotEnabledReceiver.class), PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
        isEnabled = true;
    }
}


public static void disable() {
    if(isEnabled) {
        HotspotChangeReceiver.disable();
        MainActivity.getMpackageManager().setComponentEnabledSetting(new ComponentName(MainActivity.getMcontext(), HotspotEnabledReceiver.class), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
        isEnabled = false;
    }
}
}

和禁用热点时的broadcastreceiver如下:

public class HotspotDisabledReceiver extends BroadcastReceiver implements Configurable {
public static boolean isEnabled = false;

@Override
public void onReceive(Context context, Intent intent) {
    //TODO handle properly
    if("net.wecodelicious.intent.action.HOTSPOT_DISABLED".equals(intent.getAction())) {
        Toast.makeText(context, "Hotspot Disabled ", Toast.LENGTH_LONG).show();
    }
}

public static void enable() {
    if(!isEnabled) {
        HotspotChangeReceiver.enable();
        MainActivity.getMpackageManager().setComponentEnabledSetting(new ComponentName(MainActivity.getMcontext(), HotspotDisabledReceiver.class), PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
        isEnabled = true;
    }
}


public static void disable() {
    if(isEnabled) {
        HotspotChangeReceiver.disable();
        MainActivity.getMpackageManager().setComponentEnabledSetting(new ComponentName(MainActivity.getMcontext(), HotspotDisabledReceiver.class), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
        isEnabled = false;
    }
}
}

我也在启用ManiActivity中的所有接收器组件:

public class MainActivity extends AppCompatActivity {
public static boolean isF = true;


@Override
protected void onCreate(Bundle savedInstanceState) {
...
enableall();
}

public static void enableall(){


    if(isF) {
        HotspotChangeReceiver.enable();
        HotspotEnabledReceiver.enable();
        HotspotDisabledReceiver.enable();
        isF=false;
    }
}

我已在清单文件中注册了它们:

<receiver android:name=".HotspotChangeReceiver" android:enabled="false">
        <intent-filter>
            <action android:name="android.net.wifi.WIFI_AP_STATE_CHANGED"/>
        </intent-filter>
    </receiver>
    <receiver android:name=".HotspotEnabledReceiver" android:enabled="false">
        <intent-filter>
            <action android:name="net.wecodelicious.intent.action.HOTSPOT_ENABLED"/>
        </intent-filter>
    </receiver>
    <receiver android:name=".HotspotDisabledReceiver" android:enabled="false">
        <intent-filter>
            <action android:name="net.wecodelicious.intent.action.HOTSPOT_DISABLED"/>
        </intent-filter>
    </receiver>

但是,当我运行应用程序并转动Toast上的热点时,按以下顺序显示:

Hotspot Changed
Hotspot Disabled
Hotspot Enabled

当我关闭Toasts后关闭热点时显示:

Hotspot Changed
Hotspot Disabled
Hotspot Changed
Hotspot Disabled

我希望有人认识到这种奇怪的行为,并且有一个快速解决方案。我试图找到解决方案,但无法提出任何帮助。任何帮助都将受到赞赏。

1 个答案:

答案 0 :(得分:0)

启用和禁用时,它同时是状态更改,不是它。我会注册启用和禁用,或仅注册状态更改。