一、背景
由于谷歌为了强推Firebase,所以Android 10及以上版本就把获取imei的方法给屏蔽了。这就意味着用户无法获取imei号作为设备的唯一标示了。但是阿里系使用了一套Utdid的的方案当做设备的唯一标示。UtditDemo
二、测试代码
DeviceInfo
public class DeviceInfo {
private static final String TAG = DeviceInfo.class.getSimpleName();
private static Context mContext = null;
public static void init(Context context) {
mContext = context;
}
public static String getDeviceModel() {
return Build.MODEL;
}
public static String getDeviceID() {
return Build.SERIAL;
}
public static String getOSver() {
String oSVer = "";
try {
oSVer += Build.VERSION.BASE_OS + "_";
oSVer += Build.VERSION.RELEASE;
} catch (Exception e) {
oSVer += "_" + e.getMessage();
}
return oSVer;
}
public static String getBrand() {
return Build.BRAND;
}
public static String getSn() {
//if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
// return "";
//}
try {
TelephonyManager TelephonyMgr = (TelephonyManager) mContext.getSystemService(TELEPHONY_SERVICE);
return TelephonyMgr.getDeviceId();
} catch (Exception ex) {
return "";
}
}
public static int getMemTotal() {
if (mContext != null) {
try {
final ActivityManager activityManager = (ActivityManager) mContext.getSystemService(ACTIVITY_SERVICE);
ActivityManager.MemoryInfo info = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(info);
int totalMem = (int) (info.totalMem / 1024);
return totalMem;
} catch (Exception e) {
//当android4.0以下,totalMem不支持
return 0;
}
} else
return 0;
}
public static double getMemRatio() {
if (mContext != null) {
try {
final ActivityManager activityManager = (ActivityManager) mContext.getSystemService(ACTIVITY_SERVICE);
ActivityManager.MemoryInfo info = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(info);
if (info.totalMem != 0) {
return ((double) info.availMem) / ((double) info.totalMem);
}
} catch (Exception e) {
return 0;
}
}
return 0;
}
public static double getCpuRatio() {
try {
RandomAccessFile reader = new RandomAccessFile("/proc/stat", "r");
String load = reader.readLine();
String[] toks = load.split(" ");
long idle1 = Long.parseLong(toks[5]);
long cpu1 = Long.parseLong(toks[2]) + Long.parseLong(toks[3]) + Long.parseLong(toks[4])
+ Long.parseLong(toks[6]) + Long.parseLong(toks[7]) + Long.parseLong(toks[8]);
try {
Thread.sleep(360);
} catch (Exception e) {
}
reader.seek(0);
load = reader.readLine();
reader.close();
toks = load.split(" ");
long idle2 = Long.parseLong(toks[5]);
long cpu2 = Long.parseLong(toks[2]) + Long.parseLong(toks[3]) + Long.parseLong(toks[4])
+ Long.parseLong(toks[6]) + Long.parseLong(toks[7]) + Long.parseLong(toks[8]);
return (float) (cpu2 - cpu1) / ((cpu2 + idle2) - (cpu1 + idle1));
} catch (IOException ex) {
Log.e(TAG, "get cpu usage failed!!!");
// ex.printStackTrace();
return 0;
}
// return 0;
}
public static String getCpuArch() {
return System.getProperty("os.arch");
}
/**
* 获取当前手机系统语言。
*
* @return 返回当前系统语言。例如:当前设置的是“中文-中国”,则返回“zh-CN”
*/
public static String getSystemLanguage() {
return Locale.getDefault().getLanguage();
}
/**
* 获取当前系统上的语言列表(Locale列表)
*
* @return 语言列表
*/
public static Locale[] getSystemLanguageList() {
return Locale.getAvailableLocales();
}
/**
* 获取当前手机系统版本号
*
* @return 系统版本号
*/
public static String getSystemVersion() {
return android.os.Build.VERSION.RELEASE;
}
/**
* 获取手机型号
*
* @return 手机型号
*/
public static String getSystemModel() {
return android.os.Build.MODEL;
}
/**
* 获取手机厂商
*
* @return 手机厂商
*/
public static String getDeviceBrand() {
return android.os.Build.BRAND;
}
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;
}
public static String getRefIMEI() {
String imei = "";
try {
TelephonyManager tm = (TelephonyManager) mContext.getSystemService(TELEPHONY_SERVICE);
Method method = tm.getClass().getMethod("getImei");
imei = (String) method.invoke(tm);
} catch (Exception e) {
e.printStackTrace();
}
return imei;
}
@TargetApi(Build.VERSION_CODES.M)
public static String JudgeSIM() {
String text="";
TelephonyManager tm = (TelephonyManager) mContext.getSystemService(TELEPHONY_SERVICE);
//获取当前SIM卡槽数量
int phoneCount = tm.getPhoneCount();
//获取当前SIM卡数量
int activeSubscriptionInfoCount = SubscriptionManager.from(mContext).getActiveSubscriptionInfoCount();
List<SubscriptionInfo> activeSubscriptionInfoList = SubscriptionManager.from(mContext).getActiveSubscriptionInfoList();
if(activeSubscriptionInfoList == null){
return "";
}
for(SubscriptionInfo subInfo : activeSubscriptionInfoList){
Log.d("utdid","sim卡槽位置:"+subInfo.getSimSlotIndex());
text=text+"sim卡槽位置:"+subInfo.getSimSlotIndex()+"\n";
try {
Method method = tm.getClass().getMethod("getImei",int.class);
String imei = (String) method.invoke(tm,subInfo.getSimSlotIndex());
Log.d("utdid","sim卡imei:"+imei);
text=text+"sim卡:"+subInfo.getSimSlotIndex()+" imei号:"+imei+"\n";
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
Log.d("utdid","卡槽数量:" + phoneCount);
Log.d("utdid","当前SIM卡数量:" + activeSubscriptionInfoCount);
return text;
}
@SuppressLint({"MissingPermission", "ObsoleteSdkInt", "HardwareIds"})
public static String getIMEI(){
if(mContext==null) return "";
String imei = "";
try {
TelephonyManager tm = (TelephonyManager) mContext.getSystemService(TELEPHONY_SERVICE);
if(tm == null) return "";
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { //5.0
imei = tm.getDeviceId();
} else {
Method method = tm.getClass().getMethod("getImei");
imei = (String) method.invoke(tm);
}
} catch (Exception e) {
e.printStackTrace();
}
return imei;
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
tools:context=".MainActivity">
<TextView
android:id="@+id/tvInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity
public class MainActivity extends AppCompatActivity {
private TextView tvInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestPermissions();
tvInfo = findViewById(R.id.tvInfo);
StringBuilder sb = new StringBuilder();
sb.append("Utdid值为:");
sb.append("\n");
sb.append(getUtdid());
sb.append("\n");
DeviceInfo.init(this);
sb.append("\n");
sb.append("正常获取IMEI值为:");
sb.append("\n");
sb.append(DeviceInfo.getSn());
sb.append("\n");
sb.append("\n");
sb.append("反射获取IMEI值为:");
sb.append("\n");
sb.append(DeviceInfo.getRefIMEI());
sb.append("\n");
sb.append("\n");
sb.append("遍历卡槽:");
sb.append("\n");
sb.append(DeviceInfo.JudgeSIM());
// sb.append("\n");
// sb.append("\n");
// sb.append("测试:");
// sb.append("\n");
// sb.append(DeviceInfo.getIMEI());
tvInfo.setText(sb);
DeviceInfo.JudgeSIM();
}
private void requestPermissions() {
ArrayList<String> ps = new ArrayList<>();
int per = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE);
if (per != PackageManager.PERMISSION_GRANTED) {
ps.add(Manifest.permission.READ_PHONE_STATE);
}
if (!ps.isEmpty()) {
String[] ps3 = new String[ps.size()];
ps.toArray(ps3);
ActivityCompat.requestPermissions(this, ps3, 100);
}
}
private String getUtdid() {
String utdid = com.ut.device.UTDevice.getUtdid(this);
Log.i("utdid", "getUtdid:" + utdid);
return utdid;
}
}
网友评论