Android Q 10 连接网络 WifiNetworkSpecifier

Android Q 10 Connect to network WifiNetworkSpecifier(Android Q 10 连接网络 WifiNetworkSpecifier)

本文介绍了Android Q 10 连接网络 WifiNetworkSpecifier的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于 Android Q 不允许 WifiManager 添加网络,他们建议改用 WifiNetworkSpecifier.使用 WifiNetworkSuggestionBuilder 我已经能够在状态栏上显示通知,该用户可以加入网络.但是这个 API 不能满足我的要求,因为我不需要用户使用状态栏的建议.

Since Android Q doesn't allow the WifiManager to add Networks, they gave the advise to use WifiNetworkSpecifier instead. With the WifiNetworkSuggestionBuilder I was already able to display the notification on the statusbar, that user can join the network. But this API doesn't fulfill my requirements since that I don't the user to have to use the suggestion from the statusbar.

使用 WifiNetworkSpecifier,我还已经能够显示有关加入网络的弹出窗口,并且该应用还建立了与该应用的连接.但该 wifi 连接似乎仅在应用程序范围内可用.如何克服应用程序的这个范围,以便其他应用程序(例如浏览器)能够使用这个新建立的连接?下面是我的代码

With WifiNetworkSpecifier I was also already able to display a popup about joining the network and the app also established a connection to the app. But that wifi connection seems only be available in the scope of the app. How is it possible to overcome this scope of the app, so other apps and for example also the browser is able to use this new established connection? Below is my code

    WifiNetworkSpecifier.Builder builder = new WifiNetworkSpecifier.Builder();
    builder.setSsid("abcdefgh");
    builder.setWpa2Passphrase("1234567890");

    WifiNetworkSpecifier wifiNetworkSpecifier = builder.build();
    NetworkRequest.Builder networkRequestBuilder = new NetworkRequest.Builder();
networkRequestBuilder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);     
networkRequestBuilder.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED);            
networkRequestBuilder.addCapability(NetworkCapabilities.NET_CAPABILITY_TRUSTED);            
networkRequestBuilder.setNetworkSpecifier(wifiNetworkSpecifier);
NetworkRequest networkRequest = networkRequestBuilder.build();
    ConnectivityManager cm = (ConnectivityManager) App.getInstance().getBaseContext().getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm != null) {
        cm.requestNetwork(networkRequest, new ConnectivityManager.NetworkCallback() {
            @Override
            public void onAvailable(@NonNull Network network) {
                //Use this network object to Send request.
                //eg - Using OkHttp library to create a service request

                super.onAvailable(network);
            }
        });

推荐答案

聚会有点晚了,但也许它会帮助遇到这个问题的其他人.

A bit late to the party, but maybe it will help someone else that encounters this problem.

看来你是对的.在android Q中,一旦应用被杀死,系统会自动断开我们通过WifiNetworkSpecifier连接的WiFi网络,并且无法阻止系统这样做.

It seems like you are right. In android Q once the app is killed, the system automatically disconnects the WiFi network we've connected to through WifiNetworkSpecifier and there is no way to prevent the system from doing so.

我想出的最佳解决方案是将 WifiNetworkSpecifier 与 WifiNetworkSuggestion 结合使用.这允许我们在使用我们的应用程序时使用 WifiNetworkSpecifier,并建议系统在由于应用程序终止而与我们的 WiFi 断开连接时自动连接到系统的 wifi 网络.

The best solution I've come up with, is using WifiNetworkSpecifier with WifiNetworkSuggestion. This allows us to use WifiNetworkSpecifier while we are using our app, and suggest wifi networks for the system to auto connect to once the system disconnects from our WiFi due to the app being terminated.

这里是一些示例代码:

    WifiNetworkSuggestion.Builder builder = new WifiNetworkSuggestion.Builder()
        .setSsid("YOUR_SSID")
        .setWpa2Passphrase("YOUR_PASSWORD")
    WifiNetworkSuggestion suggestion = builder.build();

    ArrayList<WifiNetworkSuggestion> list = new ArrayList<>();
    list.add(suggestion);

    WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    int status = manager.addNetworkSuggestions(list);

    if (status == STATUS_NETWORK_SUGGESTIONS_SUCCESS) {
        //We have successfully added our wifi for the system to consider
    }

干杯,

这篇关于Android Q 10 连接网络 WifiNetworkSpecifier的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Android Q 10 连接网络 WifiNetworkSpecifier

基础教程推荐