package com.example.materialtest;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.Nullable;
import android.telecom.PhoneAccountHandle;
import android.telecom.TelecomManager;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
/**
* Created by jzhou on 2017/12/8.
*/
public class SimCardUtils {
public static int getSimCardCount(Context context) {
TelephonyManager mTelephonyManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);//得到电话管理器实例
Class cls = mTelephonyManager.getClass(); //得到Class
try {
Method mMethod = cls.getMethod("getSimCount");//getSimCount方法反射
mMethod.setAccessible(true);
return (int) mMethod.invoke(mTelephonyManager);//反射的方法调用
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return -1;
}
public static int getAvailableSimCardCount(Context context){
int count = 0;
if(isMultiSim(context)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { //版本在21及以上
SubscriptionManager mSubscriptionManager = SubscriptionManager.from(context);
for (int i = 0; i < getSimCardCount(context); i++) {
SubscriptionInfo sir = mSubscriptionManager
.getActiveSubscriptionInfoForSimSlotIndex(i);
if (sir != null) {
count |= 1= 2;
}
}
return result; //true--多卡,false---单卡
}
// 这个用于获取sim数量
public static void call(Context context, int id, String telNum, @Nullable String action){
TelecomManager telecomManager = null;
List phoneAccountHandleList = null;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//版本在21及以上
telecomManager = (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE);
if(telecomManager != null) {
phoneAccountHandleList = telecomManager.getCallCapablePhoneAccounts();
}
}
Intent intent = new Intent();
intent.setAction(action);
intent.setData(Uri.parse(telNum));
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//版本在23及以上
if (phoneAccountHandleList != null) {
intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandleList.get(id));
}
}
context.startActivity(intent);
}
}
可指定电话卡拔打电话
关注
打赏
