Ane*_*s U 27 networking android android-6.0-marshmallow
我想使用连接管理器,它提供方法getAllNetworkInfo()来检查Android中的网络可用性.此方法在API级别23中已弃用.而Developer doc建议使用getAllNetworks().我试过但是没有得到我从旧代码中获得的确切功能.请有人指导我如何使用getAllNetworks()方法.以下是我正在使用的代码:
public boolean isConnectingToInternet(){
ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
@SuppressWarnings("deprecation")
NetworkInfo[] info = connectivity.getAllNetworkInfo();
//use getAllNetworks() instead
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
Mao*_*dad 29
当我更新我已弃用的代码并仍然想要支持后向Api时.我用这个:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.WANTED API VERSION){
//code
}else{
//code
}
Run Code Online (Sandbox Code Playgroud)
通过这种方式,每个设备都使用适当的代码.例:
public class ConnectionDetector {
private Context mContext;
public ConnectionDetector(Context context) {
this.mContext = context;
}
/**
* Checking for all possible internet providers
* **/
public boolean isConnectingToInternet() {
ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Network[] networks = connectivityManager.getAllNetworks();
NetworkInfo networkInfo;
for (Network mNetwork : networks) {
networkInfo = connectivityManager.getNetworkInfo(mNetwork);
if (networkInfo.getState().equals(NetworkInfo.State.CONNECTED)) {
return true;
}
}
}else {
if (connectivityManager != null) {
//noinspection deprecation
NetworkInfo[] info = connectivityManager.getAllNetworkInfo();
if (info != null) {
for (NetworkInfo anInfo : info) {
if (anInfo.getState() == NetworkInfo.State.CONNECTED) {
LogUtils.d("Network",
"NETWORKNAME: " + anInfo.getTypeName());
return true;
}
}
}
}
}
Toast.makeText(mContext,mContext.getString(R.string.please_connect_to_internet),Toast.LENGTH_SHORT).show();
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
Yak*_*pan 14
我已经制作了可以帮助你检查的工具:
它根据运行平台使用旧的或新的API:
import android.annotation.TargetApi;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.os.Build;
import android.support.annotation.NonNull;
public class NetworkUtils {
public static boolean isConnected(@NonNull Context context) {
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
return (networkInfo != null && networkInfo.isConnected());
}
public static boolean isWifiConnected(@NonNull Context context) {
return isConnected(context, ConnectivityManager.TYPE_WIFI);
}
public static boolean isMobileConnected(@NonNull Context context) {
return isConnected(context, ConnectivityManager.TYPE_MOBILE);
}
private static boolean isConnected(@NonNull Context context, int type) {
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
NetworkInfo networkInfo = connMgr.getNetworkInfo(type);
return networkInfo != null && networkInfo.isConnected();
} else {
return isConnected(connMgr, type);
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static boolean isConnected(@NonNull ConnectivityManager connMgr, int type) {
Network[] networks = connMgr.getAllNetworks();
NetworkInfo networkInfo;
for (Network mNetwork : networks) {
networkInfo = connMgr.getNetworkInfo(mNetwork);
if (networkInfo != null && networkInfo.getType() == type && networkInfo.isConnected()) {
return true;
}
}
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
更新:
有关@TargetApi
和的更多信息@RequiresApi
:https
:
//stackoverflow.com/a/40008157/421467 https://developer.android.com/reference/kotlin/androidx/annotation/RequiresApi
尝试以下代码:
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
Network[] networks = connectivityManager.getAllNetworks();
NetworkInfo networkInfo;
Network network;
for (int i = 0; i < networks.length; i++){
network = networks[i];
networkInfo = connectivityManager.getNetworkInfo(network);
if ((networkInfo.getType() == ConnectivityManager.TYPE_WIFI) && (networkInfo.getState().equals(NetworkInfo.State.CONNECTED))) {
ConnectivityManager.setProcessDefaultNetwork(network);
break;
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
27899 次 |
最近记录: |