直接封装出来一个工具类,记录一下android
package com.dreamroom.device;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;
import android.telephony.TelephonyManager;
public class SimUtils {
public JSONArray getAllSimInfo(TelephonyManager tel) throws Exception {
Class> clazz = tel.getClass();
//获取能够进行反射的字段
List 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