我正在编写代码以通过Android应用程序中的应用程序连接WiFi。但是在Android P中,当我从设备上卸载应用程序时,它也会断开我通过该应用程序连接的WiFi。卸载应用程序后,WiFi直到Android 8才断开连接,但是在Android 9中,WiFi断开连接。
代码:
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
String ssid="some_ssid";
String user ="some_user";
String password="some_password";
WifiManager wifimanager=(WifiManager) appContext.getSystemService(Context.WIFI_SERVICE);
WifiConfiguration wc= new WifiConfiguration();
wifimanager.setWifiEnabled(true);
WifiEnterpriseConfig enterpriseConfig = new WifiEnterpriseConfig();
wc.SSID = "\""+ssid+"\"";
wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X);
enterpriseConfig.setIdentity(user);
enterpriseConfig.setPassword(password);
enterpriseConfig.setCaCertificate(null);
enterpriseConfig.setEapMethod(WifiEnterpriseConfig.Eap.PEAP);
enterpriseConfig.setPhase2Method(WifiEnterpriseConfig.Phase2.NONE);
wc.enterpriseConfig = enterpriseConfig;
wifimanager.disconnect();
int netId = wifimanager.addNetwork(wc);
wifimanager.enableNetwork(netId, true);
wifimanager.reconnect();
该代码在Android 8中运行良好,但在Android 9中,当我卸载应用程序时,WiFi断开了连接。