我在使用BroadcastReceiver从IntentService访问WifiManager数据时遇到问题
MyIntentService的BroadcastReceiver可以监视WiFi状态的变化,并在连接时尝试获取当前的SSID
当应用可见并且WiFi连接{dis,}时,一切正常,但是在后台,MyIntentService和BroadcastReceiver会启动,但WifiManager对象不完整(缺少SSID,BSSID不正确,而其他字段是礼物)
相关代码段:
public class MyIntentService extends IntentService {
private final static String TAG = MyIntentService.class.getSimpleName();
static BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String methodName = new Object() {
}.getClass().getEnclosingMethod().getName();
Log.d(TAG, "-> " + methodName);
String currentSSID = SSIDManager.getCurrentSSID(context);
Log.d(TAG, "-> currentSSID: " + currentSSID);
[ ... ]
class SSIDManager {
private final static String TAG = SSIDManager.class.getSimpleName();
static String getCurrentSSID(Context context) {
WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
assert wifiManager != null;
WifiInfo info = wifiManager.getConnectionInfo();
Log.d("*****", String.valueOf(info));
[ ... ]
应用程序处于前台时的Logcat:
07-29 10:41:35.934 20094-20094/com.pijupiju.familymode D/*****: SSID: DialUp, BSSID: e8:de:27:d1:0a:dc, MAC: 02:00:00:00:00:00, Supplicant state: COMPLETED, RSSI: -32, Link speed: 54Mbps, Frequency: 2462MHz, Net ID: 7, Metered hint: false, score: 60
应用程序在后台且服务处于活动状态时的Logcat:
07-29 10:41:54.774 20094-20094/com.pijupiju.familymode D/*****: SSID: , BSSID: 02:00:00:00:00:00, MAC: 02:00:00:00:00:00, Supplicant state: COMPLETED, RSSI: -127, Link speed: -1Mbps, Frequency: -1MHz, Net ID: -1, Metered hint: false, score: 0
TL; DR-wifiManager对象已初始化,但不完整...
有什么想法吗?