我的目标是在改变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的状态时,广播接收器被称为随机时间。因此,log是打印随机时间。我只需要一次。它工作正常所有Android版本仍然是Android 4.1.0。或更高版本(果冻豆)。我使用android.net.wifi.WIFI_STATE_CHANGED。但仍然会发生同样的错误。
答案 0 :(得分: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”,然后重试。
答案 1 :(得分:4)
每当wifi的请求者状态改变时,就接收广播。由于在建立连接的过程中,这将多次改变,预期会有多个广播。 (我记得在2.3设备上也见过这个,但我主要使用连接更改广播检查连接变化,所以我可能不正确)。
您可以做的解决方法是,在广播接收器中检查附加内容包括指示SupplicantState的意图。如果extra中的请求者状态等于SupplicantState.COMPLETED(wifi已连接并通过身份验证),则只实现您的app逻辑,否则忽略广播。