83.android 简单的获取手机SIM卡卡槽数量,当前SIM卡数量。
生活随笔
收集整理的這篇文章主要介紹了
83.android 简单的获取手机SIM卡卡槽数量,当前SIM卡数量。
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
//第一步 先加入權限,在AndroidManifest.xml里加入:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />//動態權限申請:
//權限初始化 private int READ_PHONE_STATE = 1; private String[] CALLS_STATE = {Manifest.permission.READ_PHONE_STATE,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.RECORD_AUDIO, }; /** 方法名:verifyStoragePermissions(Activity activity)* 功 能:6.0動態權限* 參 數:Activity activity* 返回值:無*/ public void verifyStoragePermissions(Activity activity) {int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.READ_PHONE_STATE);if (permission != PackageManager.PERMISSION_GRANTED) {ActivityCompat.requestPermissions(activity, CALLS_STATE, READ_PHONE_STATE);} }//在Activity里調用:
verifyStoragePermissions(this);//調用請求6.0權限的方法//好了 權限申請完成后開始獲取SIM卡槽數量,可用SIM卡數量:
@TargetApi(Build.VERSION_CODES.M) @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP_MR1) private void JudgeSIM() {TelephonyManager tm = (TelephonyManager)getContext().getSystemService(TELEPHONY_SERVICE);int phoneCount = tm.getPhoneCount(); //獲取當前SIM卡槽數量//獲取當前SIM卡數量int activeSubscriptionInfoCount = SubscriptionManager.from(getContext()).getActiveSubscriptionInfoCount();Toast.makeText(getContext(), "卡槽數量:"+phoneCount, Toast.LENGTH_SHORT).show();Toast.makeText(getContext(), "當前SIM卡數量:"+activeSubscriptionInfoCount, Toast.LENGTH_SHORT).show(); }sim卡信息工具類:
/*** 獲取sim卡信息** @author CJF*/ public class SimCardUtil {@SuppressLint("StaticFieldLeak")private volatile static SimCardUtil simCardUtil = null;private final String TAG = "SimCardUtil";public static SimCardUtil getInstance() {if (null == simCardUtil) {synchronized (SimCardUtil.class) {if (null == simCardUtil) {simCardUtil = new SimCardUtil();}}}return simCardUtil;}/*** 獲取sim卡信息*/public List<SubscriptionInfo> getCallingSimCard() {if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {SubscriptionManager mSubscriptionManager = SubscriptionManager.from(context);if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {return new ArrayList<>();}List<SubscriptionInfo> list = mSubscriptionManager.getActiveSubscriptionInfoList();for (SubscriptionInfo info : list) {String iccId = info.getIccId();Log.d(TAG, " iccId:" + iccId);Log.d(TAG, " info:" + info.toString());}return list;}return new ArrayList<>();}/*** 獲取當前sim卡數量** @return*/public int getSimCardCount() {if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {SubscriptionManager from = SubscriptionManager.from(context);//獲取當前SIM卡數量if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {return 0;}int activeSubscriptionInfoCount = from.getActiveSubscriptionInfoCount();Log.d(TAG, "當前SIM卡數量:" + activeSubscriptionInfoCount);return activeSubscriptionInfoCount;}return 0;}/*** 是否雙卡** @return 雙卡true 不是雙卡false*/public boolean isTwoSimCards() {return getSimCardCount() == 2;}/*** 手機是否有sim卡存在** @return 不存在true 存在false*/public boolean isSimCardsExists() {return getSimCardCount() == 0;}/*** sim卡1是否存在** @return true存在 false不存在*/public boolean isSim1CardExist() {if (isSimCardsExists()) {return false;}if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {List<SubscriptionInfo> infoList = getCallingSimCard();for (SubscriptionInfo info : infoList) {if (info.getSimSlotIndex() == 0) {return true;}}}return false;}/*** sim卡2是否存在** @return true存在 false不存在*/public boolean isSim2CardExist() {if (isSimCardsExists()) {return false;}if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {List<SubscriptionInfo> infoList = getCallingSimCard();for (SubscriptionInfo info : infoList) {if (info.getSimSlotIndex() == 1) {return true;}}}return false;}/*** 獲取sim1運營商名稱** @return*/public String getSim1CarrierName() {if (isSimCardsExists()) {return null;}if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {List<SubscriptionInfo> infoList = getCallingSimCard();for (SubscriptionInfo info : infoList) {if (info.getSimSlotIndex() == 0) {return info.getCarrierName().toString();}}}return null;}/*** 獲取sim2運營商名稱** @return*/public String getSim2CarrierName() {if (isSimCardsExists()) {return null;}if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {List<SubscriptionInfo> infoList = getCallingSimCard();for (SubscriptionInfo info : infoList) {if (info.getSimSlotIndex() == 1) {return info.getCarrierName().toString();}}}return null;}/*** 獲取指定sim卡訂閱Id** @param slotId sim卡* @return subscriptionId*/public int getSimSubscriptionId(int slotId) {if (isSimCardsExists()) {return 0;}if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {List<SubscriptionInfo> infoList = getCallingSimCard();for (SubscriptionInfo info : infoList) {if (info.getSimSlotIndex() == slotId) {return info.getSubscriptionId();}}}return 0;}/*** 獲取當前傳遞的iccid是屬于哪張sim卡** @param iccId*/public int getSimSlotIndex(String iccId) {if (null == iccId) {return 0;}if (iccId.length() == 1) {return Integer.parseInt(iccId);}if (isSimCardsExists()) {return 0;}if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {List<SubscriptionInfo> infoList = getCallingSimCard();for (SubscriptionInfo info : infoList) {if (iccId.equals(info.getIccId())) {return info.getSimSlotIndex();}}}return 0;}/*** 獲取sim卡對應的iccid** @return*/public String getSimIccid(int slotId) {if (isSimCardsExists()) {return "";}if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {List<SubscriptionInfo> infoList = getCallingSimCard();for (SubscriptionInfo info : infoList) {if (info.getSimSlotIndex() == slotId) {return info.getIccId();}}}return "";}}?
//--------------------------------------------------------------------完--------------------------------------------------------------------------------------
總結
以上是生活随笔為你收集整理的83.android 简单的获取手机SIM卡卡槽数量,当前SIM卡数量。的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于计算机应用梦想作文,我为梦想而努力7
- 下一篇: 淘宝获取sku详细信息