Android 获取IMEI(Android 10以下可用)

阿木木 阿木木 | 135 | 2022-12-12

1、获取手机IMEI,从android 5.0之后通过getImei获取

public static String getIMEI(Context context){
		String imei = "";
		try {
			TelephonyManager tm = (TelephonyManager) context.getSystemService(TELEPHONY_SERVICE);
			if(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP){
				imei = tm.getDeviceId();
			}else {
				Method method = tm.getClass().getMethod("getImei");
				imei = (String) method.invoke(tm);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return imei;
	}

2、判断哪个卡槽有插卡,从api 23开始,api23之前需要反射来获取,方法名也有所差别

  @TargetApi(Build.VERSION_CODES.M)
    public static void JudgeSIM(Context context) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(TELEPHONY_SERVICE);
        //获取当前SIM卡槽数量
        int phoneCount = tm.getPhoneCount();
        //获取当前SIM卡数量
        int activeSubscriptionInfoCount = SubscriptionManager.from(context).getActiveSubscriptionInfoCount();
		List<SubscriptionInfo> activeSubscriptionInfoList = SubscriptionManager.from(context).getActiveSubscriptionInfoList();
		if(activeSubscriptionInfoList == null){
			return;
		}
		for(SubscriptionInfo subInfo : activeSubscriptionInfoList){
			Logger.d("sim卡槽位置:"+subInfo.getSimSlotIndex());
			try {
				Method method = tm.getClass().getMethod("getImei",int.class);
				String imei = (String) method.invoke(tm,subInfo.getSimSlotIndex());
				Logger.d("sim卡imei:"+imei);
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}

		}
		Logger.d("卡槽数量:" + phoneCount);
        Logger.d("当前SIM卡数量:" + activeSubscriptionInfoCount);
    }
文章标签: Android
推荐指数:

真诚点赞 诚不我欺~

Android 获取IMEI(Android 10以下可用)

点赞 收藏 评论