Android Q 10连接到网络WifiNetworkSpecifier [英] Android Q 10 Connect to network WifiNetworkSpecifier

查看:3493
本文介绍了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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆