【发布时间】:2018-10-11 22:56:32
【问题描述】:
在版本 (7.0) 之后执行此操作的最佳方法是什么 我正在使用“BroadcastReceiver”执行此操作,但在版本(7.0)之后开始出现问题 我尝试使用“GcmTaskService”和“JobScheduler” 但是我没有找到正确的方法。 谁能帮帮我?
【问题讨论】:
标签: android
在版本 (7.0) 之后执行此操作的最佳方法是什么 我正在使用“BroadcastReceiver”执行此操作,但在版本(7.0)之后开始出现问题 我尝试使用“GcmTaskService”和“JobScheduler” 但是我没有找到正确的方法。 谁能帮帮我?
【问题讨论】:
标签: android
RxAndroid (https://github.com/ReactiveX/RxAndroid) 为响应异步进程提供了非常有用的工具。
具体与网络连接有关,您可以使用内置的 android NetworkManager 和 WifiConfiguration 开始连接,并使用 RxAndroid ReactiveNetwork 静态方法观察连接。
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();
【讨论】: