android on connect to network listener后台
在版本(7.0)之后,最好的方法是什么 我使用“BroadcastReceiver”进行此操作,但在版本(7.0)之后,问题开始出现 我尝试使用“GcmTaskService”和“JobScheduler” 但是我没有找到正确的路。 有人能帮我吗?RxAndroid()为响应异步进程提供了非常有用的工具 特别是关于网络连接,您可以使用内置的android NetworkManager和Wific配置开始连接,并使用RxAndroid反应网络静态方法观察连接android on connect to network listener后台,android,Android,在版本(7.0)之后,最好的方法是什么 我使用“BroadcastReceiver”进行此操作,但在版本(7.0)之后,问题开始出现 我尝试使用“GcmTaskService”和“JobScheduler” 但是我没有找到正确的路。 有人能帮我吗?RxAndroid()为响应异步进程提供了非常有用的工具 特别是关于网络连接,您可以使用内置的android NetworkManager和Wific配置开始连接,并使用RxAndroid反应网络静态方法观察连接 WifiConfiguration w
WifiConfiguration wifiConfig = new WifiConfiguration();
conf.ssid = "\"SecureNetwork\"" //Note that quote characters are required in the SSID
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
WifiManager wifiManger = (WifiManager) appContext.getSystemService(Context.WIFI_SERVICE);
if (wifiManager == null) {
//error handling - make sure you have permissions etc
}
int connectionId = wifiManager.addNetwork(conf);
if (connectionId == -1) {
//Network is already configured - find configuration using wifiManger.getConfiguredNetworks();
}
wifiManager.enableNetwork(connectionId, true); //Attempt to connect to network
//Now use RxAndroid ReactiveNetwork to observe connectivity
ReactiveNetwork.observeNetworkConnectivity(context)
.filter(ConnectivityPredicate.hasState(NetworkInfo.State.CONNECTED))
.filter(ConnectivityPredicate.hasType(ConnectivityManager.TYPE_WIFI))
.flatMap(connectivity -> {
int currentNetId = wifiManager.getConnectionInfo().getNetworkId();
if (currentNetId == connectionNetId) {
return Observable.just(true);
} else {
return Observable.error("Not connected to new network")
}
.doOnNext(mBoolean -> //Do whatever you want to now that you are connect)
.doOnError(mError ->> //Handle error)
.suscribe();
RxAndroid()为响应异步进程提供了非常有用的工具
特别是关于网络连接,您可以使用内置的android NetworkManager和Wific配置开始连接,并使用RxAndroid反应网络静态方法观察连接
WifiConfiguration wifiConfig = new WifiConfiguration();
conf.ssid = "\"SecureNetwork\"" //Note that quote characters are required in the SSID
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
WifiManager wifiManger = (WifiManager) appContext.getSystemService(Context.WIFI_SERVICE);
if (wifiManager == null) {
//error handling - make sure you have permissions etc
}
int connectionId = wifiManager.addNetwork(conf);
if (connectionId == -1) {
//Network is already configured - find configuration using wifiManger.getConfiguredNetworks();
}
wifiManager.enableNetwork(connectionId, true); //Attempt to connect to network
//Now use RxAndroid ReactiveNetwork to observe connectivity
ReactiveNetwork.observeNetworkConnectivity(context)
.filter(ConnectivityPredicate.hasState(NetworkInfo.State.CONNECTED))
.filter(ConnectivityPredicate.hasType(ConnectivityManager.TYPE_WIFI))
.flatMap(connectivity -> {
int currentNetId = wifiManager.getConnectionInfo().getNetworkId();
if (currentNetId == connectionNetId) {
return Observable.just(true);
} else {
return Observable.error("Not connected to new network")
}
.doOnNext(mBoolean -> //Do whatever you want to now that you are connect)
.doOnError(mError ->> //Handle error)
.suscribe();
可能的重复可能的重复