Android TelephonyManager双卡获取数据开启状态异常的可能原因

背景

应用内不指定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()}
 *
 * 

Requires 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}). * *

Note 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 system's 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);
}

Android TelephonyManager双卡获取数据开启状态异常的可能原因_第1张图片

3、SubscriptionController的逻辑,未指定subid时拿到的会是voice的 能力

Android TelephonyManager双卡获取数据开启状态异常的可能原因_第2张图片

使用方法和说明

在Android中,TelephonyManager类的isDataEnabled()方法用于检查移动数据连接是否启用。而subId(Subscription ID)是用于标识不同SIM卡的唯一ID。

Note:isDataEnable()可不传入参数,即默认的subId(使用getDefaultDataSubscriptionId()方法获取),也可以指定subId(如下代码示例)。

  1. 在多卡手机中,可以通过TelephonyManager的getSubId()方法获取当前活动的SIM卡的SubId,
  2. 然后可使用TelephonyManager.isDataEnabled()方法检查指定的subId对应的SIM卡的移动数据链接是否启用

代码示例:
 

TelephonyManager  mTelephonyManager = (TelephonyManager) getSystemService(context.TELEPHONY_SERVICE);

int subId = 1; //要检查的SIM的subId

if(mTelephonyManager.isDataEnabled(subId)) {
    //移动数据已启用
} else {
    //移动数据未启用
}

代码链接参考:

  • TelephonyManager.isDataEnable()
  • SubscriptionManager.getDefaultDataSubscriptionId()
  • SubscriptionController.getDefaultSubId()

你可能感兴趣的:(Android,AOSP,android,数据状态)