我正在開發一個沒有GUI的應用程序。當Wifi按鈕被改變(ON或OFF)時,它應該開始它的服務。 這是我的清單文件:如何捕捉無線網絡變化?
<receiver android:name="com.updater.wifitester.NetWatcher">
<intent-filter
android:priority="100">
<action
android:name="android.net.wifi.WIFI_STATE_CHANGED"
android:enabled="true"
/>
</intent-filter>
</receiver>
下面是我給的權限:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
我擴展的廣播接收器:
public class NetWatcher extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) {
//check intents and service to know if all is ok then set bWifiStateOk accordingly
Toast.makeText(context, "Wifi on", Toast.LENGTH_LONG).show();
Log.i("wifi_state_changed","wifi changed...");
} else {
return ; // do nothing ... we're not in good intent context doh !
}
}
}
但有沒有在日誌貓並沒有顯示Toasts。我錯過了什麼? 請幫我傢伙...
檢查:http://stackoverflow.com/q/10733121/1739882 –