SIM卡管理

来源:互联网 发布:java jar命令 编辑:程序博客网 时间:2024/05/16 06:31

//获取TelephonyManager实例对象 
final TelephonyManager tm = (TelephonyManager) getActivity().getSystemService(Context.TELEPHONY_SERVICE); 
//获取SIM卡个数 
final int numSlots = tm.getSimCount();

public static SubscriptionInfo findRecordBySlotId(Context context, final int slotId) { 
//由SubscriptionManager对象获取SubscriptionInfo列表 
final List subInfoList = 
SubscriptionManager.from(context).getActiveSubscriptionInfoList(); 
if (subInfoList != null) { 
final int subInfoLength = subInfoList.size(); 
//sir.getSimSlotIndex()可获取SubscriptionInfo的卡槽位置 
for (int i = 0; i < subInfoLength; ++i) { 
final SubscriptionInfo sir = subInfoList.get(i); 
if (sir.getSimSlotIndex() == slotId) { 
//Right now we take the first subscription on a SIM. 
return sir; 


}

    return null;}

//获取phoneAccount对象,当其为null时说明未设置当前打电话时使用的是哪个sim卡,当用户已设置时该对象将不会为null 
final PhoneAccountHandle phoneAccount = telecomManager.getUserSelectedOutgoingPhoneAccount(); 
private void updateCallValues() { 
final Preference simPref = findPreference(KEY_CALLS); 
if (simPref != null) { 
final TelecomManager telecomManager = TelecomManager.from(getActivity()); 
final PhoneAccountHandle phoneAccount = 
telecomManager.getUserSelectedOutgoingPhoneAccount(); 
simPref.setTitle(R.string.calls_title); 
simPref.setSummary(phoneAccount == null 
? getResources().getString(R.string.sim_calls_ask_first_prefs_title) 
: (String)telecomManager.getPhoneAccount(phoneAccount).getLabel()); 
int accoutSum = telecomManager.getCallCapablePhoneAccounts().size(); 
Log.d(TAG, “accountSum: ” + accoutSum + “PhoneAccount: ” + phoneAccount); 
simPref.setEnabled(accoutSum >= 1); 

}

//可获取当前用来发送短信的SIM卡的subId 
mSubscriptionManager.getDefaultSmsSubId()); 
private void updateSmsValues() { 
final Preference simPref = findPreference(KEY_SMS); 
if (simPref != null) { 
final SubscriptionInfo sir = Utils.findRecordBySubId(getActivity(), 
mSubscriptionManager.getDefaultSmsSubId()); 
simPref.setTitle(R.string.sms_messages_title); 
if (DBG) log(“[updateSmsValues] mSubInfoList=” + mSubInfoList);

        if (sir != null) {            simPref.setSummary(sir.getDisplayName());        } else if (sir == null) {            updateSmsSummary(simPref);        }        simPref.setEnabled(mSelectableSubInfos.size() >= 1);     }}

//设置哪张卡来发送短信 
subscriptionManager.setDefaultSmsSubId(subId);

//获取当前使用数据流量的sim卡的subId 
SubscriptionManager.from(mContext).getDefaultDataSubId()

//Android L 后双卡项目设置用哪张卡来使用数据流量和打开数据流量是分别设置的 
//设置是否打开数据流量 
TelephonyManager mTelephonyManager = TelephonyManager.from(getActivity()); 
mTelephonyManager.setDataEnabled(enabled); 
//设置哪张卡来使用数据流量 
subscriptionManager.setDefaultDataSubId(subId);

1 0
原创粉丝点击