由于Android Q不允许WifiManager添加网络,他们建议改用WifiNetworkSpecifier。使用WifiNetworkSuggestionBuilder,我已经能够在状态栏上显示通知,该用户可以加入网络。但是这个API不能满足我的需求,因为我不想让用户使用statusbar中的建议。
有了WifiNetworkSpecifier,我已经能够显示一个关于加入网络的弹出窗口,应用程序也建立了与应用程序的连接。但这种wifi连接似乎只在应用程序的范围内可用。如何才能克服应用程序的这个范围,这样其他应用程序和浏览器都能够使用这个新建立的连接呢?下面是我的代码
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);
}
});
发布于 2020-01-17 02:41:54
参加聚会有点晚了,但也许这会对遇到这个问题的其他人有所帮助。
看起来你是对的。在安卓Q中,一旦应用程序被杀死,系统就会自动断开我们通过WifiNetworkSpecifier连接的WiFi网络,没有办法阻止系统这样做。
我想出的最好的解决方案是在WifiNetworkSuggestion中使用WifiNetworkSpecifier。这允许我们在使用应用程序时使用WifiNetworkSpecifier,并建议系统在由于应用程序被终止而与wifi断开连接时自动连接到WiFi网络。
下面是一些示例代码:
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
}
干杯,
发布于 2020-09-03 18:10:35
我试过这段代码,它运行良好
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.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) {
super.onAvailable(network);
cm.bindProcessToNetwork(network)
}});
发布于 2021-03-05 14:53:40
您必须添加此行才能使用internet。networkRequestBuilder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
https://stackoverflow.com/questions/57929863
复制相似问题