Android 10的requestNetwork()函数执行时间过长,显示指定网络。

huangapple go评论98阅读模式
英文:

Android 10 requestNetwork() takes too long displaying specified network

问题

我正在尝试将我的 Android 10 设备连接到 WiFi 网络。我正在使用 WiFiNetworkSpecifier API 来描述我想要连接的网络属性。连接进程进行得很好,但我经常看到用户显示的提示花费的时间太长(从 2 到 28 秒不等),才会显示我用 WifiNetworkSpecifier 对象描述的网络。

以下是我的代码(与此处链接的 Google 示例相同 -> https://developer.android.com/guide/topics/connectivity/wifi-bootstrap

ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkRequest networkRequest = new NetworkRequest.Builder()
        .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
        .removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
        .setNetworkSpecifier(
                new WifiNetworkSpecifier.Builder()
                        .setSsid(SSID)
                        .setWpa2Passphrase(psw)
                        .build()
        )
        .build();

networkCallback = new ConnectivityManager.NetworkCallback() {
   @Override
   public void onAvailable(@NonNull Network network) {
       if (WiFiCoordinator.this.listner != null){
           WiFiCoordinator.this.listner.onConnected();
       }

       cm.bindProcessToNetwork(network);
   }

   @Override
   public void onUnavailable() {
       super.onUnavailable();
       listner.onTestNetworkNotAvailable();
   }
};
cm.requestNetwork(networkRequest, networkCallback);

连接本身没有问题,但操作系统寻找所请求的网络所花费的时间对我来说并不理想。在我的代码中是否存在任何问题?

非常感谢。

英文:

I'm trying to connect my Android 10 device to a WiFi network. I'm using the WiFiNetworkSpecifier API to describe the network properties I want to connect to. The connection goes well, but I see often that the prompt shown to the user is taking too long (from 2 to 28 seconds) to display the network I described with WifiNetworkSpecifier object.

Here is my code (it is the same as the Google example linked here -> https://developer.android.com/guide/topics/connectivity/wifi-bootstrap)

ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkRequest networkRequest = new NetworkRequest.Builder()
                .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
                .removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
                .setNetworkSpecifier(
                        new WifiNetworkSpecifier.Builder()
                                .setSsid(SSID)
                                .setWpa2Passphrase(psw)
                                .build()
                )
                .build();

            networkCallback = new ConnectivityManager.NetworkCallback() {
               @Override
               public void onAvailable(@NonNull Network network) {
                   if (WiFiCoordinator.this.listner != null){
                       WiFiCoordinator.this.listner.onConnected();
                   }

                   cm.bindProcessToNetwork(network);
               }

               @Override
               public void onUnavailable() {
                   super.onUnavailable();
                   listner.onTestNetworkNotAvailable();
               }
            };
            cm.requestNetwork(networkRequest, networkCallback);

The connection has no problem, but the time spent by the OS looking for the requested network is not ok for me. Is there any problem in my code?

Thanks a lot

答案1

得分: 1

通过比较实验发现,Android 11 的表现与 Android 9 相当。

我猜是 Android 10 的一个错误。比较 Android 11 和 Android 10 的代码,我们发现在请求网络过程中存在差异。

英文:

Through comparative experiments, it is found that Android 11 can work as well as Android 9

I guess it's the Android 10 bug. Comparing the code of Android 11 and Android 10, we find that there is a difference in the request network process.

huangapple
  • 本文由 发表于 2020年8月25日 20:56:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63579338.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定