android 反射获取sim卡json
时间: 2023-11-12 08:06:04 浏览: 85
以下是获取 SIM 卡信息的示例代码,将其转换为 JSON 格式:
```java
import android.content.Context;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.List;
public class SimCardUtils {
public static JSONObject getSimCardInfo(Context context) throws JSONException {
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
SubscriptionManager subscriptionManager = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
JSONArray jsonArray = new JSONArray();
List<SubscriptionInfo> subscriptionInfoList = subscriptionManager.getActiveSubscriptionInfoList();
if (subscriptionInfoList != null) {
for (SubscriptionInfo subscriptionInfo : subscriptionInfoList) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("simSerialNumber", subscriptionInfo.getIccId());
jsonObject.put("carrierName", subscriptionInfo.getCarrierName().toString());
jsonObject.put("countryIso", subscriptionInfo.getCountryIso());
jsonObject.put("displayName", subscriptionInfo.getDisplayName().toString());
jsonObject.put("number", subscriptionInfo.getNumber());
jsonObject.put("subscriberId", subscriptionInfo.getSubscriptionId());
jsonObject.put("mcc", telephonyManager.getSimOperator(subscriptionInfo.getSubscriptionId()).substring(0, 3));
jsonObject.put("mnc", telephonyManager.getSimOperator(subscriptionInfo.getSubscriptionId()).substring(3));
jsonArray.put(jsonObject);
}
}
JSONObject result = new JSONObject();
result.put("simCards", jsonArray);
return result;
}
}
```
调用 `getSimCardInfo()` 方法即可获取 SIM 卡信息的 JSON 格式。注意需要在 AndroidManifest.xml 文件中声明读取 SIM 卡信息的权限:
```xml
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
```
阅读全文