gpt4 book ai didi

java - 缺少 Android 网络连接状态。

转载 作者:太空狗 更新时间:2023-10-29 14:25:03 26 4
gpt4 key购买 nike

我正在尝试评估我的手机连接到我的 wifi 网络的能力。我想知道验证我的 wifi 接入点需要多长时间,获取 IP 地址需要多长时间等等。

感谢这个网站和大量测试,我取得了很大进步。但是,我永远无法让我的广播接收器触发某些连接状态。我真的很想以某种方式检测身份验证发生的时间。

现在我知道 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 之间走来走去,断开 wifi 并重新连接等等...

我还尝试过连接接收器来接收我能接收到的任何信息。

我使用了这个 intent 过滤器:

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 专家可以在这方面帮助我?理想情况下,我希望获得一些我可以使用的工作代码,但即使是一些关于为什么从未达到这种状态的有用信息也会非常有帮助。这是一个错误吗?

谢谢!

最佳答案

对我有用:

IntentFilter mIntentFilter = new IntentFilter();
mIntentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
registerReceiver(receiverWifi, mIntentFilter);


class WifiReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context c, Intent intent) {
String action = intent.getAction();
if(action.equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)){
Log.d("WifiReceiver", ">>>>SUPPLICANT_STATE_CHANGED_ACTION<<<<<<");
SupplicantState supl_state=((SupplicantState)intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE));
switch(supl_state){
case ASSOCIATED:Log.i("SupplicantState", "ASSOCIATED");
break;
case ASSOCIATING:Log.i("SupplicantState", "ASSOCIATING");
break;
case AUTHENTICATING:Log.i("SupplicantState", "Authenticating...");
break;
case COMPLETED:Log.i("SupplicantState", "Connected");
break;
case DISCONNECTED:Log.i("SupplicantState", "Disconnected");
break;
case DORMANT:Log.i("SupplicantState", "DORMANT");
break;
case FOUR_WAY_HANDSHAKE:Log.i("SupplicantState", "FOUR_WAY_HANDSHAKE");
break;
case GROUP_HANDSHAKE:Log.i("SupplicantState", "GROUP_HANDSHAKE");
break;
case INACTIVE:Log.i("SupplicantState", "INACTIVE");
break;
case INTERFACE_DISABLED:Log.i("SupplicantState", "INTERFACE_DISABLED");
break;
case INVALID:Log.i("SupplicantState", "INVALID");
break;
case SCANNING:Log.i("SupplicantState", "SCANNING");
break;
case UNINITIALIZED:Log.i("SupplicantState", "UNINITIALIZED");
break;
default:Log.i("SupplicantState", "Unknown");
break;

}
int supl_error=intent.getIntExtra(WifiManager.EXTRA_SUPPLICANT_ERROR, -1);
if(supl_error==WifiManager.ERROR_AUTHENTICATING){
Log.i("ERROR_AUTHENTICATING", "ERROR_AUTHENTICATING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
}
}
}

<receiver
android:name=".MyActivity$WifiReceiver"
android:process=":remote" >
</receiver>

关于java - 缺少 Android 网络连接状态。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13318646/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com