android获取NetworkMode

阅读 69

2022-08-01

public String getNetworkMode(Context context) {

ConnectivityManager conMan = (ConnectivityManager) context

.getSystemService(Context.CONNECTIVITY_SERVICE);



// mobile 3G Data Network



State mobile = State.UNKNOWN;

try {

mobile = conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)

.getState();

} catch (Exception e) {

}



// wifi

State wifi = State.UNKNOWN;

try {

wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI)

.getState();

} catch (Exception e) {

}



if (wifi == State.CONNECTED) {

return "wifi";

} else if (mobile == State.CONNECTED) {

return "mobile";

} else {

return "";

}

}

精彩评论(0)

0 0 举报