洪梅镇仿做网站,网站关键字统计,公司网站忘了怎么做,免费建域名网站背景
应用内不指定subId获取数据状态可能会错误#xff0c;因为可能拿到voice的能力#xff0c;而非data。
代码逻辑
1、通过TelephonyManager的isDataEnabled()没有指定subId时#xff0c;调用内部方法isDataEnabledForReason#xff0c;传入getId()参数以指定subid因为可能拿到voice的能力而非data。
代码逻辑
1、通过TelephonyManager的isDataEnabled()没有指定subId时调用内部方法isDataEnabledForReason传入getId()参数以指定subid然后会执行到SubscriptionManager的getDefaultDataSubscriptionId()以获取默认值
/*** Returns whether mobile data is enabled or not per user setting. There are other factors* that could disable mobile data, but they are not considered here.** If this object has been created with {link #createForSubscriptionId}, applies to the given* subId. Otherwise, applies to {link SubscriptionManager#getDefaultDataSubscriptionId()}** pRequires one of the following permissions:* {link android.Manifest.permission#ACCESS_NETWORK_STATE},* {link android.Manifest.permission#MODIFY_PHONE_STATE}, or* {link android.Manifest.permission#READ_BASIC_PHONE_STATE* READ_BASIC_PHONE_STATE} or that the calling app has carrier* privileges (see {link #hasCarrierPrivileges}).** pNote that this does not take into account any data restrictions that may be present on the* calling app. Such restrictions may be inspected with* {link ConnectivityManager#getRestrictBackgroundStatus}.** return true if mobile data is enabled.*/
RequiresPermission(anyOf {android.Manifest.permission.ACCESS_NETWORK_STATE,android.Manifest.permission.MODIFY_PHONE_STATE,android.Manifest.permission.READ_PHONE_STATE,android.Manifest.permission.READ_BASIC_PHONE_STATE})
RequiresFeature(PackageManager.FEATURE_TELEPHONY_DATA)
public boolean isDataEnabled() {try {return isDataEnabledForReason(DATA_ENABLED_REASON_USER);} catch (IllegalStateException ise) {// TODO(b/176163590): Remove this catch once TelephonyManager is booting safely.Log.e(TAG, Error calling #isDataEnabled, returning default (false)., ise);return false;}
}2、根据SubscriptionManager逻辑会查询获取DefaultSubId的值最终用的是SubscriptionController数据
/*** Returns the systems default data subscription id.** On a voice only device or on error, will return INVALID_SUBSCRIPTION_ID.** return the default data subscription Id.*/
public static int getDefaultDataSubscriptionId() {return sDefaultDataSubIdCache.query(null);
} 3、SubscriptionController的逻辑未指定subid时拿到的会是voice的 能力 使用方法和说明
在Android中TelephonyManager类的isDataEnabled()方法用于检查移动数据连接是否启用。而subIdSubscription ID是用于标识不同SIM卡的唯一ID。
NoteisDataEnable()可不传入参数即默认的subId使用getDefaultDataSubscriptionId()方法获取也可以指定subId如下代码示例。
在多卡手机中可以通过TelephonyManager的getSubId()方法获取当前活动的SIM卡的SubId然后可使用TelephonyManager.isDataEnabled()方法检查指定的subId对应的SIM卡的移动数据链接是否启用
代码示例
TelephonyManager mTelephonyManager (TelephonyManager) getSystemService(context.TELEPHONY_SERVICE);int subId 1; //要检查的SIM的subIdif(mTelephonyManager.isDataEnabled(subId)) {//移动数据已启用
} else {//移动数据未启用
} 代码链接参考
TelephonyManager.isDataEnable()SubscriptionManager.getDefaultDataSubscriptionId()SubscriptionController.getDefaultSubId()