我試圖註冊一個Wifi BroadcastReceiver,以便在更改時獲取wifi狀態。但到目前爲止,我沒有收到廣播的運氣。清單中聲明的BroadcastReceiver未收到廣播
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<receiver android:name=".WifiReceiver" >
<intent-filter>
<action android:name="android.net.wifi.WifiManager.WIFI_STATE_CHANGED_ACTION" />
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
//activity declaration here...
</application>
你們知道如何在清單中註冊BraodcastReceiver嗎?
我並不想註冊活動,因爲我想監控無線時候有我的應用程序是否運行,或者是wifi的狀態變化。
這是我的廣播接收器類:
public class WifiReceiver extends BroadcastReceiver {
private final String TAG = "WifiReceiver";
@Override
public void onReceive(Context context, Intent intent) {
int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);
String wifiStateText = "No State";
switch (wifiState) {
case WifiManager.WIFI_STATE_DISABLING:
wifiStateText = "WIFI_STATE_DISABLING";
break;
case WifiManager.WIFI_STATE_DISABLED:
wifiStateText = "WIFI_STATE_DISABLED";
break;
case WifiManager.WIFI_STATE_ENABLING:
wifiStateText = "WIFI_STATE_ENABLING";
break;
case WifiManager.WIFI_STATE_ENABLED:
wifiStateText = "WIFI_STATE_ENABLED";
break;
case WifiManager.WIFI_STATE_UNKNOWN:
wifiStateText = "WIFI_STATE_UNKNOWN";
break;
default:
break;
}
MyLog.d(TAG, "onReceive Broadcast > WiFiState: " + wifiStateText);
MyLog.d(TAG, "onReceive Broadcast > Time: " + new Date());
}
}
我真的希望能得到一些幫助。提前致謝。
看這個博客http://marakana.com/forums/android/examples/40.html – Ajay 2012-02-24 04:34:18
你有沒有爲Android定義權限的所有廣播接收器.permission.ACCESS_WIFI_STATE和android.permission.CHANGE_WIFI_STATE – Ajay 2012-02-24 04:36:38
@Ajay,我已經看過你提到的博客。問題就像我剛纔提到的那樣,我想在清單中註冊接收者而不是在活動中。我已經在清單中放入了許可。無論如何,謝謝你的關注Ajay。 – Zul 2012-02-24 07:00:10