当前位置: 首页 > news >正文

Android中获取手机SIM卡的各种信息

 通过以下工具类方法可以获取到手机SIM的各种信息数据!!!

package com.utils;
import android.telephony.TelephonyManager;
import com.baidu.platform.comapi.map.E;
import org.json.JSONArray;
import org.json.JSONObject;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;/*** 使用方法* TelephonyManager tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);* SONArray simList = SimUtils.getAllSimInfo(tel);*/
public class SimUtils {public static JSONArray getAllSimInfo(TelephonyManager tel) throws Exception {Class clazz = tel.getClass();//获取能够进行反射的字段List<EMethod> list = new ArrayList<>();Map listIgnore = new HashMap<>();Method[] methods = clazz.getDeclaredMethods();for (Method method : methods) {String name = method.getName();if (!name.startsWith("get"))continue;if (listIgnore.get(name) != null)continue;listIgnore.put(name, 0);Method m1 = null;Method m2 = null;Method m3 = null;try {m1 = clazz.getDeclaredMethod(name);} catch (Exception e) {}try {m2 = clazz.getDeclaredMethod(name, int.class);} catch (Exception e) {}try {m3 = clazz.getDeclaredMethod(name, long.class);} catch (Exception e) {}if (m1 != null && ((m2 == null && m3 != null) || (m2 != null && m3 == null))) {Class c1 = m1.getReturnType();Class c2 = m2 == null ? null : m2.getReturnType();Class c3 = m3 == null ? null : m3.getReturnType();if (m2 == null) {if (c1 == null || c1 != c3)continue;} else {if (c1 == null || c1 != c2)continue;}EMethod item = new EMethod(name, m2 == null ? 1 : 0, c1);list.add(item);}}listIgnore.clear();JSONArray array = new JSONArray();for (int i = 0; i < 10; i++) {JSONObject json = new JSONObject();for (EMethod em : list) {Method method = null;Object param = null;if (em.type == 0) {method = clazz.getDeclaredMethod(em.name, int.class);param = i;} else {method = clazz.getDeclaredMethod(em.name, long.class);param = new Long(i);}if (!method.isAccessible())method.setAccessible(true);String name = em.name.substring(3);Object value = null;try {value = method.invoke(tel, param);} catch (Exception e) {//前面已经对private设置了可访问,有些仍是会报错,就无论这个了continue;}json.put(name, value);}if (json.optInt("SimState") == TelephonyManager.SIM_STATE_UNKNOWN || json.optInt("SimState") == TelephonyManager.SIM_STATE_ABSENT)continue;String imsi = json.optString("SubscriberId");if (imsi == null || imsi.length() == 0)continue;//根据imsi去重boolean repeact = false;for (int j = 0; j < array.length(); j++) {if (imsi.equals(array.optJSONObject(j).optString("SubscriberId"))) {repeact = true;break;}}if (repeact)continue;array.put(json);}return array;}static class EMethod {public String name;public int type;//0为int,1为longpublic Class returnType;//返回类型public EMethod(String name, int type, Class returnType) {this.name = name;this.type = type;this.returnType = returnType;}}
}

http://www.lryc.cn/news/146407.html

相关文章:

  • matlab 根据索引提取点云
  • 蓝芯、四川邦辰面试(部分)
  • openCV实战-系列教程13:文档扫描OCR识别下(图像轮廓/模版匹配)项目实战、源码解读
  • SpringBootWeb案例 Part 4
  • 什么是ChatGPT水印,ChatGPT生成的内容如何不被检测出来,原理什么?
  • Android 6.0 Settings中添加虚拟键开关
  • Yolov8小目标检测(12):动态稀疏注意力BiFormer | CVPR 2023
  • C# VS调试技巧
  • VS的调试技巧
  • lucene国内镜像 极速下载
  • Qt 信号槽连接方式
  • (线程池) 100行以内的简单线程池
  • Mysql按姓氏从小到大排序的正确sql
  • 【C++】详细介绍模版初阶—函数模版、类模板
  • BananaPi BPI-6202工业控制板全志科技A40i、24V DC输入、RS485接口
  • Python - functools.partial设置回调函数处理异步任务基本使用
  • phpspreadsheet导出excel自动获得列,数字下标
  • 结算日-洛谷
  • Android Native Code开发学习(一)环境配置
  • Python GUI应用程序开发之wxPython使用详解
  • 【电子学会真题】青少年软件编程(C语言)等级考试试卷(一级) 2021年9月
  • 学习完毕JavaSE的感想
  • FastJson的学习
  • python scrapy框架
  • 滑动窗口系列3-Leetcode134题加油站
  • LOIC(low orbit ion cannon)
  • 从格灵深瞳中报稳定盈利,看AI公司的核心竞争力
  • 理解 Databend Cluster key 原理及使用
  • C++day3(类、this指针、类中的特殊成员函数)
  • Qt中的配置文件:实现个性化应用程序配置与保存加载