检查IMEI地址和字符串时出错

时间:2016-12-12 11:42:17

标签: android device imei

我尝试比较IMEI代码和字符串

public static String getDeviceIMEI(Context context) {
    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
    }
    String identifier = null;
    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    if (tm != null)
        identifier = tm.getDeviceId();
    if (identifier == null || identifier.length() == 0)
        identifier = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);

    return identifier;
}

此方法计算设备的IMEI代码,它返回带有代码的String。 这个方法比较2个字符串:

private boolean checkIMEI() {        
    if (Device.getDeviceIMEI(SplashScreenActivity.this) == "xxx")
        return true;
    else
        return false;
}

(“xxx”是我设备的个人IMEI地址)

但是当我在设备上运行它时,方法checkIMEI()会返回给我false ... 我没有任何想法来解决这个问题。 谢谢你的帮助

2 个答案:

答案 0 :(得分:0)

您正在使用==比较Java中的字符串来检查它是否是同一个对象,而不是相同的字符串,您可以使用.equals(),如下所示:

private boolean checkIMEI() {        
    if (Device.getDeviceIMEI(SplashScreenActivity.this).equals("xxx"))
        return true;
    else
        return false;
}

请参阅How do I compare strings in Java?

答案 1 :(得分:0)

public static String getIMEI(Context context) {
    TelephonyManager TelephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    return TelephonyMgr.getDeviceId();
}