4

我的目标是在更改 wi-fi 状态时打印日志。

我使用下面的代码。

MainActivity.java(主活动)

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startService(new Intent(this,WiFiService.class));
    }
    @Override
    protected void onStart() 
    {
        super.onStart();
        Log.d("Start Service", "Start Service");;
        startService(new Intent(this,WiFiService.class));
    }
}

WiFiService.java(服务)

public class WiFiService extends Service
{

    WiFiBroadCasetReceiver brod;
    @Override
    public IBinder onBind(Intent intent) 
    {
        return null;
    }
    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        brod=new WiFiBroadCasetReceiver();
        this.registerReceiver(brod, new IntentFilter(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION));
    }

    @Override
    public void onDestroy() 
    {
        super.onDestroy();
    }
}

WiFiBroadCasetReceiver.java (BroadcastReceiver)

public class WiFiBroadCasetReceiver extends BroadcastReceiver
{

    @Override
    public void onReceive(Context arg0, Intent arg1) 
    {
        Log.d("on receiver", "receiver");
    }

}

AndroidManifest.xml

<application>
        ........
        ........

        <receiver android:name=".WiFiBroadCasetReceiver" >
            <intent-filter>
                <action android:name="android.net.wifi.supplicant.STATE_CHANGE" />
            </intent-filter>
        </receiver>
</application>

问题 :

以上代码在 android 4.0 及更低版本中运行良好。当我改变 wi-fi 的状态时,广播接收器是随机调用的。所以,日志是随机打印的。我只需要一次。它工作正常所有 android 版本仍然是 android 4.1.0。或更高版本(果冻豆)。我使用 android.net.wifi.WIFI_STATE_CHANGED。但仍然发生同样的错误。

4

2 回答 2

5

您的问题应该解决几个问题。

首先,您可能会混淆“android.net.wifi.supplicant.STATE_CHANGE”和“android.net.wifi.WIFI_STATE_CHANGED”的状态。我认为你真正想要的是后者。请参阅源代码中的注释。

/**
 * Broadcast intent action indicating that Wi-Fi has been enabled, disabled,
 * enabling, disabling, or unknown. One extra provides this state as an int.
 * Another extra provides the previous state, if available.
 *
 * @see #EXTRA_WIFI_STATE
 * @see #EXTRA_PREVIOUS_WIFI_STATE
 */
@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
public static final String WIFI_STATE_CHANGED_ACTION =
    "android.net.wifi.WIFI_STATE_CHANGED";

/**
 * Broadcast intent action indicating that a connection to the supplicant has
 * been established (and it is now possible
 * to perform Wi-Fi operations) or the connection to the supplicant has been
 * lost. One extra provides the connection state as a boolean, where {@code true}
 * means CONNECTED.
 * @see #EXTRA_SUPPLICANT_CONNECTED
 */
@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
public static final String SUPPLICANT_CONNECTION_CHANGE_ACTION =
    "android.net.wifi.supplicant.CONNECTION_CHANGE";

第二,为什么你有多个广播回调?而且次数是随机的?我认为您可能需要仔细检查您的代码:

1. You start the service twice, once in Activity.onCreate() and once in Activity.onStart()
2. You register your broadcast receiver twice, once in AndroidManifest.xml and once in Service.onStart()
3. The most important thing is that you will create a new instance of your broadcast receiver instance in your Service.onStart(). That is to say, whenever your service is start, a new receiver will be created and registered. And looking back on 1, you see every time you bring you Activity back will call the service to start again.

所以回调的随机时间是因为你的代码不好。请删除所有广播寄存器,只留下 AndroidManifest.xml 中的一个

最后,为什么以后不能让它在 JellyBean 上工作?我认为这是因为您没有指定正确的操作。请尝试“android.net.wifi.WIFI_STATE_CHANGED”而不是“android.net.wifi.supplicant.STATE_CHANGE”,然后重试。

于 2013-08-13T11:19:17.593 回答
4

每当 wifi 的请求者状态发生变化时,就会收到广播。由于在建立连接的过程中这将发生多次变化,因此预计会有多个广播。(我记得在 2.3 设备上也看到过这种情况,但我主要使用连接更改广播来检查连接更改,所以我可能不正确)。

您可以做的解决方法是,在您的广播接收器中检查额外包含在指示 SupplicantState 的意图中。如果额外中的请求者状态等于 SupplicantState.COMPLETED(wifi 已连接并经过身份验证),则仅实现您的应用程序逻辑,否则忽略广播。

于 2013-08-05T09:15:38.937 回答