您当前的位置: 首页 > 

仙剑情缘

暂无认证

  • 1浏览

    0关注

    333博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

可指定电话卡拔打电话

仙剑情缘 发布时间:2018-04-07 16:14:29 ,浏览量:1

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);
    }



}
关注
打赏
1658017818
查看更多评论
立即登录/注册

微信扫码登录

0.0407s