1

我是 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;
    }
}
}

禁用热点时的广播接收器如下:

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>

但是当我运行应用程序并打开热点时,Toasts 会按以下顺序显示:

Hotspot Changed
Hotspot Disabled
Hotspot Enabled

当我关闭热点时,也会显示以下 Toasts:

Hotspot Changed
Hotspot Disabled
Hotspot Changed
Hotspot Disabled

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

4

1 回答 1

0

当您启用和禁用时,它同时是一个状态更改,不是吗。我要么注册启用和禁用,要么只注册状态更改本身。

于 2016-09-29T19:52:31.077 回答