我不知道为什么当我连接或断开wifi时,在wifi接收器的下面发布的代码中相应的回调,被调用至少三次,并且
因此,如果我想根据当前的wifi状态调用一个函数,那么该函数将被多次调用,我不希望这样的行为,特别是在
中当有wifi连接时应调用的函数,我正在显示一个对话框,并且该调用将被多次调用,对话框将是 多次调用会导致重叠。
请在下面发布的logcat中找到,这只是我得到的一个例子。
请让我知道为什么wifi状态的相应回调被多次调用,以及如何避免这种多次调用?
码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
tv_status = (TextView) findViewById(R.id.tv_status);
this.registerReceiver(this.myWifiReceiver, new IntentFilter(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION));
}
private BroadcastReceiver myWifiReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.d(TAG, "@onReceive()");
mConnMgr = (ConnectivityManager)getSystemService(network_service);
WifiManager wifiMgr = (WifiManager) context.getSystemService(WiFi_Service);
switch (wifiMgr.getWifiState()) {
case WifiManager.WIFI_STATE_ENABLED:
Log.d(TAG, "wifi enabled");
if (mConnMgr != null) {
networkInfo = mConnMgr.getActiveNetworkInfo();
if (networkInfo != null) {
switch(networkInfo.getType()) {
case ConnectivityManager.TYPE_WIFI:
if (networkInfo.isConnected()) {
tv_status.setText("Connected");
Log.d(TAG, "Connected");
}else if (networkInfo.isConnectedOrConnecting()) {
tv_status.setText("Status: CONNECTED Or CONNECTING");
Log.d(TAG, "Status: CONNECTED Or CONNECTING");
}else if (networkInfo.isFailover()){
Log.d(TAG, "Status: FailOver");
}else if (networkInfo.isRoaming()) {
Log.d(TAG, "Status: Roaming");
}else if (networkInfo.isAvailable()) {
Log.d(TAG, "Status: Available");
} else {
Log.d(TAG, "Status: disconnected");
}
break;
}
}else {
tv_status.setText("Status: No Default NetWork Connected");
Log.d(TAG, "Status: No Default NetWork Connected");
}
} else {
tv_status.setText("Status: ConnMgr is null");
Log.d(TAG, "Status: ConnMgr is null");
}
break;
logcat的:
03-04 15:31:31.031: D/MainActivity(16120): @onReceive()
03-04 15:31:31.031: D/MainActivity(16120): wifi enabled
03-04 15:31:31.032: D/MainActivity(16120): Status: No Default NetWork Connected
03-04 15:31:31.546: D/MainActivity(16120): @onReceive()
03-04 15:31:31.547: D/MainActivity(16120): wifi enabled
03-04 15:31:31.547: D/MainActivity(16120): Status: No Default NetWork Connected
03-04 15:31:31.617: D/MainActivity(16120): @onReceive()
03-04 15:31:31.618: D/MainActivity(16120): wifi enabled
03-04 15:31:31.620: D/MainActivity(16120): Status: No Default NetWork Connected
03-04 15:31:31.636: D/MainActivity(16120): @onReceive()
03-04 15:31:31.636: D/MainActivity(16120): wifi enabled
03-04 15:31:31.637: D/MainActivity(16120): Status: No Default NetWork Connected
03-04 15:32:28.661: D/MainActivity(16120): @onReceive()
03-04 15:32:28.663: D/MainActivity(16120): wifi enabled
03-04 15:32:28.666: D/MainActivity(16120): Status: No Default NetWork Connected
03-04 15:32:28.708: D/MainActivity(16120): @onReceive()
03-04 15:32:28.709: D/MainActivity(16120): wifi enabled
03-04 15:32:28.710: D/MainActivity(16120): Status: No Default NetWork Connected
03-04 15:32:28.722: D/MainActivity(16120): @onReceive()
03-04 15:32:28.722: D/MainActivity(16120): wifi enabled
03-04 15:32:28.722: D/MainActivity(16120): Status: No Default NetWork Connected
Update_1 :
使用计时器的MySolution :
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
tv_status = (TextView) findViewById(R.id.tv_status);
IntentFilter intfil = new IntentFilter(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
this.registerReceiver(this.myWifiReceiver, intfil);
}
private BroadcastReceiver myWifiReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.d(TAG, "@onReceive()");
Timer t1 = new Timer();
Timer t2 = new Timer();
if (!locked) {
if (scheduled1) {
t1.cancel();
t1.purge();
}
if (scheduled2) {
t2.cancel();
t2.purge();
}
WifiManager wifiMgr = (WifiManager) context.getSystemService(WiFi_Service);
switch (wifiMgr.getWifiState()) {
case WifiManager.WIFI_STATE_ENABLED:
Log.d(TAG, "wifi enabled");
locked = true;
t1 = new Timer();
t1.schedule(MQTTReceiverLockTimedTask, 4000);
scheduled1 = true;
module();
break;
case WifiManager.WIFI_STATE_DISABLED:
Log.d(TAG, "wifi disabled");
locked = true;
t2 = new Timer();
t2.schedule(MQTTReceiverLockTimedTask, 4000);
scheduled2 = true;
break;
}
}
}
};
TimerTask MQTTReceiverLockTimedTask = new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
locked = false;
}
};
protected void module() {
// TODO Auto-generated method stub
mConnMgr = (ConnectivityManager)getSystemService(network_service);
if (mConnMgr != null) {
networkInfo = mConnMgr.getActiveNetworkInfo();
if (networkInfo != null) {
switch(networkInfo.getType()) {
case ConnectivityManager.TYPE_WIFI:
if (networkInfo.isConnected()) {
tv_status.setText("Connected");
Log.d(TAG, "Connected");
}else if (networkInfo.isConnectedOrConnecting()) {
tv_status.setText("Status: CONNECTED Or CONNECTING");
Log.d(TAG, "Status: CONNECTED Or CONNECTING");
}else if (networkInfo.isFailover()){
Log.d(TAG, "Status: FailOver");
}else if (networkInfo.isRoaming()) {
Log.d(TAG, "Status: Roaming");
}else if (networkInfo.isAvailable()) {
Log.d(TAG, "Status: Available");
} else {
Log.d(TAG, "Status: disconnected");
}
break;
}
}else {
tv_status.setText("Status: No Default NetWork Connected");
Log.d(TAG, "Status: No Default NetWork Connected");
}
} else {
tv_status.setText("Status: ConnMgr is null");
Log.d(TAG, "Status: ConnMgr is null");
}
}
答案 0 :(得分:2)
来自WifiManager documentation
WifiManager.SUPPLICANT_STATE_CHANGED_ACTION
:
广播意图操作,指示建立与接入点的连接的状态已更改。另外一个提供新的SupplicantState。
您收到许多onReceive()
的来电,因为SupplicantState
提供了有关建立连接状态的更详细信息。每次这种状态改变时,都会播放新的意图。由于您只需要一个不太详细的信息,因此您似乎多次获得相同的广播。
关于如何避免这种情况:要么搜索另一个倾听意图,例如ConnectivityManager.CONNECTIVITY_ACTION
是否符合您的需要,要么尝试Mahmoud Elmorabea的解决方案 - 将当前的WiFi连接状态保存在私有Activity类上的字段,onReceive()
上的字段,检查连接状态是否已更改。如果没有,请跳过其余代码。否则,请运行代码并更新您的状态。如果您使用此解决方案,请记住在活动生命周期中保存状态字段。
答案 1 :(得分:1)
保持WiFi的当前状态,管理器或其他东西,并且只有在调用onReceive()时使用与当前状态不同的状态才执行代码。
我们假设您在Activity中使用此代码。
在你的活动中,你将拥有一个名为currentWifiState的属性,起初它等于-100;例如
在您的接收器中,代码就像这样
if (wifiManager.getWifiState() == currentWifiState)
return;
然后,如果您收到通知的状态与当前状态相同,则不会执行任何操作。
除非Wifi状态发生变化,否则不会执行您的代码。