我有一个代码,用于侦听连接或断开热点或wifi,我在BroadCastReceiver中有两个Log我的问题是当我连接到wifi或断开wifi时它给了我很多日志可以吗帮助我为什么会这样。这是我的代码
广播接收器
package com.example.amire.yadavar;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.util.Log;
public class IsWifiConnectedBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if (wifiManager.isWifiEnabled()) Log.d("WIFI is Connected to", wifiManager.getConnectionInfo().getSSID());
else if(!wifiManager.isWifiEnabled()) Log.d("WIFI not-Connected to",wifiManager.getConnectionInfo().getSSID());
}
}
AndroidManifest
<receiver android:name=".IsWifiConnectedBroadcastReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.net.wifi.STATE_CHANGE" />
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
来自一个连接的日志
08-27 20:37:07.550 4665-4665/com.example.amire.yadavar D/WIFI is Connected to: "HUAWEI Y330-U01"
08-27 20:37:07.558 4665-4665/com.example.amire.yadavar D/WIFI is Connected to: "HUAWEI Y330-U01"
08-27 20:37:07.561 4665-4665/com.example.amire.yadavar D/WIFI is Connected to: "HUAWEI Y330-U01"
08-27 20:37:07.565 4665-4665/com.example.amire.yadavar D/WIFI is Connected to: "HUAWEI Y330-U01"
08-27 20:37:07.569 4665-4665/com.example.amire.yadavar D/WIFI is Connected to: "HUAWEI Y330-U01"
08-27 20:37:07.576 4665-4665/com.example.amire.yadavar D/WIFI is Connected to: "HUAWEI Y330-U01"
08-27 20:37:07.925 4665-4665/com.example.amire.yadavar D/WIFI is Connected to: "HUAWEI Y330-U01"
08-27 20:37:07.928 4665-4665/com.example.amire.yadavar D/WIFI is Connected to: "HUAWEI Y330-U01"
08-27 20:37:07.941 4665-4665/com.example.amire.yadavar D/WIFI is Connected to: "HUAWEI Y330-U01"
08-27 20:37:07.947 4665-4665/com.example.amire.yadavar D/WIFI is Connected to: "HUAWEI Y330-U01"
08-27 20:37:07.956 4665-4665/com.example.amire.yadavar D/WIFI is Connected to: "HUAWEI Y330-U01"
08-27 20:37:08.150 4665-4665/com.example.amire.yadavar D/WIFI is Connected to: "HUAWEI Y330-U01"
08-27 20:37:08.158 4665-4665/com.example.amire.yadavar D/WIFI is Connected to: "HUAWEI Y330-U01"
感谢您的评论。
答案 0 :(得分:0)
您将获得具有不同ACTION和不同数据的不同广播Intent
。您应该记录Intent
中的所有数据,包括所有“额外内容”。这为您提供了许多细粒度的信息。如果您不想要所有这些细粒度的信息,您可以决定使用您要查找的特定数据对Intent
做出反应,而忽略其余数据。