3
我試圖評估我的手機連接到我的無線網絡的能力。我想知道需要多長時間來驗證我的wifi接入點,需要多長時間來獲取IP地址等。缺少Android網絡連接狀態。
我已經取得了很多進展,這要歸功於這個網站以及很多測試。然而,我永遠不能縫合以讓我的廣播接收器觸發某些連接狀態。我真的想以某種方式檢測身份驗證發生的時間。
現在我知道了Android文檔所說的內容,並且認證狀態應該很容易訪問。然而,在多個設備的實踐中,該狀態從未達到。我甚至試着用下面的代碼進行輪詢。
public void start(long delayMillsec) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
long timeout = System.currentTimeMillis() + 5000;
running = true;
while(true)
{
int oldState = state;
showWifiStatus();
int newState = state;
if(newState != oldState)
{
Log.e("STATE", stateString + ": " + System.nanoTime());
}
}
} }
, delayInMillsec);
}
public void showWifiStatus() {
boolean connected = false;
boolean associated = false;
ConnectivityManager connManager = (ConnectivityManager) thisContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
DetailedState val = mWifi.getDetailedState();
if (val == NetworkInfo.DetailedState.SCANNING) {
//Log.e("Detailed Wifi Info"," wifi state is SCANNING ");
state = 0;
stateString = "SCANNING";
}
if (val == NetworkInfo.DetailedState.CONNECTING) {
//Log.e("Detailed Wifi Info"," wifi state is CONNECTING ");
associated = true;
state = 1;
stateString = "CONNECTING";
}
if (val == NetworkInfo.DetailedState.AUTHENTICATING) {
//Log.e("Detailed Wifi Info"," 3wifi state is AUTHENTICATING");
associated = true;
state = 2;
stateString = "AUTHENTICATING";
}
if (val == NetworkInfo.DetailedState.OBTAINING_IPADDR) {
//Log.e("Detailed Wifi Info"," wifi state is OBTAINING_IPADDR");
associated = true;
state = 3;
stateString = "OBTAINING_IPADDR";
}
if (val == NetworkInfo.DetailedState.CONNECTED) {
//Log.e("Detailed Wifi Info"," wifi state is CONNECTED ");
connected = true;
associated = true;
state = 4;
stateString = "CONNECTED";
}
if (val == NetworkInfo.DetailedState.DISCONNECTED) {
//Log.e("Detailed Wifi Info"," wifi state is DISCONNECTED ");
state = 5;
stateString = "DISCONNECTED";
}
if (val == NetworkInfo.DetailedState.DISCONNECTING) {
// Log.e("Detailed Wifi Info"," wifi state is DISCONNECTING ");
state = 6;
stateString = "DISCONNECTING";
}
if (val == NetworkInfo.DetailedState.FAILED) {
//Log.e("Detailed Wifi Info"," wifi state is FAILED");
state = 7;
stateString = "FAILED";
}
if (val == NetworkInfo.DetailedState.IDLE) {
//Log.e("Detailed Wifi Info"," wifi state is IDLE");
state = 8;
stateString = "IDLE";
}
if (val == NetworkInfo.DetailedState.SUSPENDED) {
//Log.e("Detailed Wifi Info"," wifi state is SUSPENDED");
state = 9;
stateString = "SUSPENDED";
}
}
當我立即撥打了啓動功能,從後臺服務,然後我走了知WiFi AP間的之間圍繞和斷開無線網絡連接並重新連接等等
我也試過連接接收器拿起我所能做的。
我用這個意圖過濾器:
IntentFilter ConnectedFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
ConnectedFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
ConnectedFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
ConnectedFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
getApplicationContext().registerReceiver(ConnectedToAPReceiver,
ConnectedFilter);
有了這個接收器,輸出正被接收絕對一切。並且從來沒有任何跡象表明認證狀態曾經被觸發過。
private BroadcastReceiver ConnectedToAPReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Bundle extras = intent.getExtras();
if (extras != null) {
Log.e("NEW Action", intent.getAction());
for (String key: extras.keySet()) {
Log.e("CONN_ACTION", "key [" + key + "]: " +
extras.get(key));
}
Log.e("NEW THING", "------------");
}
else {
Log.e("CONNACTION", "no extras");
}
}};
我使用以下權限過於:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
是否有任何Android的專家那裏誰可以幫我在這一個?理想情況下,我想獲得一些我可以使用的工作代碼,但即使是一些關於此狀態從未達到的好信息也會非常有用。這是一個錯誤?
謝謝!
我錯過了這一點:mIntentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION); 謝謝! – Dave