系统中添加imei到persist.sys.imei系统属性中
if (intent.getAction().equals("android.intent.action.SIM_STATE_CHANGED")) {
String IMEI0 = getImeiOnly(context, 0);
String IMEI1 = getImeiOnly(context, 1);
SystemProperties.set("persist.sys.imei0", IMEI0);
SystemProperties.set("persist.sys.imei1", IMEI1);
}
public String getImeiOnly(Context context, int slotId) {
String imei = "";
try {
TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
if (manager != null) {
imei = manager.getImei(slotId);
}
} catch (Exception e) {
}
if (TextUtils.isEmpty(imei)) {
TelephonyManager telephonyManager =
(TelephonyManager) context.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
String imeiOrMeid = telephonyManager.getDeviceId(slotId);//getDeviceId(context, slotId);
if (!TextUtils.isEmpty(imeiOrMeid) && imeiOrMeid.length() >= 15) {
imei = imeiOrMeid;
}
}
return imei;
}
第三方应用就可以通过SystemProperties.get来获取,SystemProperties可以用反射来调用
package com.unitech.dmitest;
import java.lang.reflect.Method;
public class PropertyUtils {
private static volatile Method set = null;
private static volatile Method get = null;
public static void set(String prop, String value) {
try {
if (null == set) {
synchronized (PropertyUtils.class) {
if (null == set) {
Class<?> cls = Class.forName("android.os.SystemProperties");
set = cls.getDeclaredMethod("set", new Class<?>[]{String.class, String.class});
}
}
}
set.invoke(null, new Object[]{prop, value});
} catch (Throwable e) {
e.printStackTrace();
}
}
public static String get(String prop, String defaultvalue) {
String value = defaultvalue;
try {
if (null == get) {
synchronized (PropertyUtils.class) {
if (null == get) {
Class<?> cls = Class.forName("android.os.SystemProperties");
get = cls.getDeclaredMethod("get", new Class<?>[]{String.class, String.class});
}
}
}
value = (String) (get.invoke(null, new Object[]{prop, defaultvalue}));
} catch (Throwable e) {
e.printStackTrace();
}
return value;
}
}
660

被折叠的 条评论
为什么被折叠?



