如何判断“移动networking数据”是启用还是禁用(即使通过WiFi连接)?

我有一个应用程序,我希望能够用来从远程查询获取连接状态报告。

我想知道WiFi是否连接,以及是否通过移动networking启用数据访问。

如果WiFi超出范围,我想知道是否可以依靠移动networking。

问题是,当我通过WiFi连接时,启用的数据总是被返回为真,并且当没有通过WiFi连接时,我只能正确查询移动networking。

我看到的所有答案都表明轮询,看看目前的连接是什么,但我想知道如果我需要移动networking,即使我目前可以通过WiFi连接。

是否有无论如何查询移动networking数据是否启用而不查询连接?

编辑

所以,当通过WiFi连接如果我去设置,并取消select“数据启用”,然后在我的应用程序,我这样做:

boolean mob_avail = conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isAvailable(); 

mob_avail返回为“真”,但我已经禁用移动networking数据,所以我希望它是'假'

如果我closuresWiFi,没有连接(正确),因为我禁用了移动networking数据。

那么如何通过WiFi连接来检查移动networking数据是否已启用?

UPDATE

我按照ss1271的build议,看了一下getAllNetworkInfo()

我在以下3个条件下输出了关于移动networking的信息

WiFi Off – 移动数据

WiFi On – 移动数据closures

WiFi On – 移动数据

并得到以下结果:

closuresWiFi:

移动[HSUPA],状态:CONNECTED / CONNECTED,原因:未知,额外:互联网,漫游:假,故障转移:假,isAvailable:真,featureId:-1,userDefault:假

随着WiFi开/移动closures

NetworkInfo:type:mobile [HSUPA],state:DISCONNECTED / DISCONNECTED,reason:connectionDisabled,extra:(none),roaming:false,failover:false,isAvailable:true,featureId:-1,userDefault:false

随着WiFi开/移动开

NetworkInfo:type:mobile [HSPA],state:DISCONNECTED / DISCONNECTED,reason:connectionDisabled,extra:(none),roaming:false,failover:false,isAvailable:true,featureId:-1,userDefault:false

因此,您可以看到isAvailable每次都返回true,状态只有在WiFi有效时显示为Disconnected。

澄清

我不想看我的手机是否目前由移动networking连接。 我试图确定用户是否启用/禁用通过移动networking的数据访问。 他们可以打开和closures打开和closures设置 – >无线和networking设置 – >移动networking设置 – >数据启用

下面的代码会告诉您是否启用了“移动数据”,无论此时是否有移动数据连接处于活动状态,或者是否启用了WiFi。 此代码仅适用于Android 2.3(姜饼)和更高版本。 其实这个代码也适用于早期版本的Android以及;-)

  boolean mobileDataEnabled = false; // Assume disabled ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); try { Class cmClass = Class.forName(cm.getClass().getName()); Method method = cmClass.getDeclaredMethod("getMobileDataEnabled"); method.setAccessible(true); // Make the method callable // get the setting for "mobile data" mobileDataEnabled = (Boolean)method.invoke(cm); } catch (Exception e) { // Some problem accessible private API // TODO do whatever error handling you want here } 

注意:你将需要有权限android.permission.ACCESS_NETWORK_STATE才能够使用这个代码。

我已经升级了Allesio的答案。 Settings.Secure的mobile_data int从4.2.2版本转移到了Settings.Global。

尝试此代码,当您想知道是否启用移动networking,即使WiFi已启用并连接。

已更新,以检查SIM卡是否可用。 感谢您指出murat。

 boolean mobileYN = false; TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); if (tm.getSimState() == TelephonyManager.SIM_STATE_READY) { if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) { mobileYN = Settings.Global.getInt(context.getContentResolver(), "mobile_data", 1) == 1; } else{ mobileYN = Settings.Secure.getInt(context.getContentResolver(), "mobile_data", 1) == 1; } } 

一种方法是检查用户是否在“设置”中激活了移动数据,如果WiFiclosures,则很可能会使用这些数据。 这工作(testing),它不使用reflection,虽然它在API中使用一个隐藏的值:

 boolean mobileDataAllowed = Settings.Secure.getInt(getContentResolver(), "mobile_data", 1) == 1; 

根据API,您需要检查Settings.Global而不是Settings.Secure,正如@ user1444325指出的那样。

来源: Android API调用来确定用户设置“数据已启用”

您必须使用ConnectivityManager,并且可以在此处findNetworkInfo详细信息

你可以尝试这样的事情:

 ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); //mobile State mobile = conMan.getNetworkInfo(0).getState(); //wifi State wifi = conMan.getNetworkInfo(1).getState(); if (mobile == NetworkInfo.State.CONNECTED || mobile == NetworkInfo.State.CONNECTING) { //mobile } else if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING) { //wifi } 

如果你有兴趣,如果你是真正连接,使用

 NetworkInfo.State.CONNECTED 

只有,而不是

 NetworkInfo.State.CONNECTED || NetworkInfo.State.CONNECTING 

我认为使用NetworkInfo类和isConnected应该工作:

 ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); return info != NULL || info.isConnected(); 

而检查移动数据也许是连接的。 我不能确定,直到我testing它。 明天之前我不能做的事情。

 TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); if(tm .getDataState() == tm .DATA_CONNECTED) return true; 
 To identify which SIM or slot is making data connection active in mobile, we need to register action android:name="android.net.conn.CONNECTIVITY_CHANGE" with permission uses-permission android:name="android.permission.CONNECTIVITY_INTERNAL" & uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" public void onReceive(Context context, Intent intent) if (android.net.conn.CONNECTIVITY_CHANGE.equalsIgnoreCase(intent .getAction())) { IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE); IConnectivityManager service = IConnectivityManager.Stub.asInterface(b); NetworkState[] states = service.getAllNetworkState(); for (NetworkState state : states) { if (state.networkInfo.getType() == ConnectivityManager.TYPE_MOBILE && state.networkInfo.isConnected()) { TelephonyManager mTelephonyManager = (TelephonyManager) context .getSystemService(Context.TELEPHONY_SERVICE); int slotList = { 0, 1 }; int[] subId = SubscriptionManager.getSubId(slotList[0]); if(mTelephonyManager.getDataEnabled(subId[0])) { // this means data connection is active for SIM1 similary you //can chekc for SIM2 by slotList[1] ................. } } } 
  ConnectivityManager cm = (ConnectivityManager) activity .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = cm.getActiveNetworkInfo(); String networkType = ""; if (info.getType() == ConnectivityManager.TYPE_WIFI) { networkType = "WIFI"; } else if (info.getType() == ConnectivityManager.TYPE_MOBILE) { networkType = "mobile"; } 

这是一个解决这个问题的Xamarin解决scheme:

  public static bool IsMobileDataEnabled() { bool result = false; try { Context context = //get your context here or pass it as a param if (Build.VERSION.SdkInt >= BuildVersionCodes.JellyBeanMr1) { //Settings comes from the namespace Android.Provider result = Settings.Global.GetInt(context.ContentResolver, "mobile_data", 1) == 1; } else { result = Settings.Secure.GetInt(context.ContentResolver, "mobile_data", 1) == 1; } } catch (Exception ex) { //handle exception } return result; } 

PS:确保你有这个代码的所有权限。

@ sNash的function很好。 但是在很less的设备中,我发现即使数据被禁用,它也会返回true。 所以我find了一个在Android API中的备用解决scheme。

TelephonyManager的 getDataState()方法将非常有用。

我用上面的函数更新了@ snash的函数。 蜂窝数据被禁用时,下面的函数返回false,否则返回true。

 private boolean checkMobileDataIsEnabled(Context context){ boolean mobileYN = false; TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); if (tm.getSimState() == TelephonyManager.SIM_STATE_READY) { TelephonyManager tel = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); // if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) // { // mobileYN = Settings.Global.getInt(context.getContentResolver(), "mobile_data", 0) == 1; // } // else{ // mobileYN = Settings.Secure.getInt(context.getContentResolver(), "mobile_data", 0) == 1; // } int dataState = tel.getDataState(); Log.v(TAG,"tel.getDataState() : "+ dataState); if(dataState != TelephonyManager.DATA_DISCONNECTED){ mobileYN = true; } } return mobileYN; } 
Interesting Posts