天天看點

可指定電話卡拔打電話

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<<i;
                    }
                }
            }
        }
        return count; //0--無有效卡,1--卡1有效,2---卡2有效,3----卡1,卡2都有效
    }

    public static boolean isMultiSim(Context context){
        boolean result = false;
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//版本在21及以上

            TelecomManager telecomManager = (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE);
            if (telecomManager != null) {
                List<PhoneAccountHandle> phoneAccountHandleList = telecomManager.getCallCapablePhoneAccounts();
                result = phoneAccountHandleList.size() >= 2;
            }
        }
        return result;    //true--多卡,false---單卡
    }
//    這個用于擷取sim數量

    public static void call(Context context, int id, String telNum, @Nullable String action){

        TelecomManager telecomManager = null;
        List<PhoneAccountHandle> 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);
    }



}