Thursday, February 27, 2014

Excuse me, how to determine the phone's data connection is enabled?


Regardless of what network currently in use, only need to know the phone settings where (or decline shortcut icon inside) is turned up
Data connection (enabled via the mobile network to access data)

My tests are currently using WIFI, but regardless of whether the phone's data connection is checked
NetworkInfo where various parameters are unchanged ah
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
NetworkInfo generally used to determine whether there is a network connection, you can determine whether there were WIFI or mobile network. You need to determine whether there should be a mobile network it, but do not want to know this option is on or off, right?
 ConnectivityManager con = (ConnectivityManager) getSystemService (Activity.CONNECTIVITY_SERVICE); 
boolean wifi = con.getNetworkInfo (ConnectivityManager.TYPE_WIFI)
. IsConnectedOrConnecting ();
boolean internet = con.getNetworkInfo (ConnectivityManager.TYPE_MOBILE)
. IsConnectedOrConnecting ();
if (wifi | internet) {
/ / Have net
flag = "0";
} Else {
/ / No network
flag = "1";
}

Reply:
Now the situation is that if TYPE_MOBILE is being used, then this option is turned on / off are able to get,
However, if the network is currently using other methods, then this option is turned on / off changes not know

In fact, I do not care about the way this is what the network, but this [data-enabled] option must be open (PS: This option is the name of the phone models may be different, but the meaning is the same)
Reply:

No idea, the next mark.
 
private static NetworkInfo getCurrentActiveNetworkInfo (Context context) {

ConnectivityManager connectivityManager = (ConnectivityManager) context

. GetSystemService (Context.CONNECTIVITY_SERVICE);

if (connectivityManager! = null)

return connectivityManager.getActiveNetworkInfo ();

return null;

}

public static int getCurrentNetType (Context context) {

int result = NETTYPE_NONE;

NetworkInfo localNetworkInfo = getCurrentActiveNetworkInfo (context);

if (localNetworkInfo == null) {

return result;

}

if (localNetworkInfo.getState () == NetworkInfo.State.CONNECTED) {

if (localNetworkInfo.getType () == 1) {

result = NETTYPE_WIFI;

} Else if (localNetworkInfo.getType () == 0) {

String subTypeName = localNetworkInfo.getSubtypeName ()

. ToUpperCase ();

if (subTypeName.indexOf ("GPRS")> 1) {

result = NETTYPE_MOBILE_GPRS;

} Else if (subTypeName.indexOf ("EDGE")> 1) {

result = NETTYPE_MOBILE_EDGE;

} Else {

result = NETTYPE_MOBILE_3G;

}

} Else {

result = NETTYPE_UNKNOW;

}

} Else if (localNetworkInfo.getState () == NetworkInfo.State.CONNECTING) {

result = NETTYPE_UNKNOW;

}

return result;

}

Reply:
Is it because of this:
Reply:
android WIFI network to connect high priority than mobile, so if the current network is WIFI, regardless of whether the check [Enable Data Connection], in fact, no real mobile connections are open, so there is no mobile network status change?
Reply:
ConnectivityManager class has a hidden function can determine the status of the mobile data network and open, you can get
reflectionHowever, this API is unstable, not ready to Kacha out when you follow the source - or write a function ~
Reply:
1L should have to meet the needs of the bar.
Reply:
Thank you for reply

Tips
by 6LConnectivityManager connectivity = (ConnectivityManager) cxt.getSystemService (Context.CONNECTIVITY_SERVICE);
try {
. Method getMobileDataEnabled = connectivity.getClass () getMethod ("getMobileDataEnabled", null);
boolean b = (Boolean) getMobileDataEnabled.invoke (connectivity, null);
Log.i ("ConnectivityManager", "getMobileDataEnabled:" + b);

/ *
Method setMobileDataEnabled = connectivity.getClass () getMethod ("setMobileDataEnabled", boolean.class);.
setMobileDataEnabled.invoke (connectivity, true);


* /
} Catch (Exception e) {
e.printStackTrace ();
}
To invoke the comment code, do not forget

android 2.2 test (no guarantee that other machines can ^ _ ^)
Results posted

No comments:

Post a Comment