0

I try to comparing the IMEI code and a string

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;
}

this method calculate the IMEI code of the device, it return a String with the code. And this method compare the 2 string:

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

("xxx" is the personal IMEI address of my device)

But when I run it on the device, the method checkIMEI() returns to me false... I don't have any kind of idea to solve the problem. Thanks for helping

Simone
  • 39
  • 8
  • in `checkIMEI()` what is the value of `Device.getDeviceIMEI(SplashScreenActivity.this)` ? (can you log it). Also, if you didn't know, that `if` block is a little weird, it doesn't contain any code within it. – Matthias Dec 12 '16 at 11:49
  • @Matthias, I log it and the value of the IMEI (from Device.getDeviceIMEI(...)) is the same value of my input ("xxx"), the xxx value is the result of Log.i("IMEI", Device.getDeviceIMEI(...)); – Simone Dec 12 '16 at 11:51
  • oh right, if memory serves, in Java to compare strings you must use `String.equals(otherString)` not `==` – Matthias Dec 12 '16 at 12:00
  • @Matthias ok, it works ! great, thank you – Simone Dec 12 '16 at 13:09
  • excellent, feel free to accept my answer as the correct one then :) – Matthias Dec 12 '16 at 13:51

2 Answers2

0

You are using == to compare strings in Java which checks to see if it is the same object, not the same string of characters, you can use .equals() for that, like so:

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

See How do I compare strings in Java?

Community
  • 1
  • 1
Matthias
  • 3,160
  • 2
  • 24
  • 38
0
public static String getIMEI(Context context) {
    TelephonyManager TelephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    return TelephonyMgr.getDeviceId();
}
BDL
  • 21,052
  • 22
  • 49
  • 55
xbadal
  • 1,284
  • 2
  • 11
  • 24
  • Although this code might solve the problem, one should always add an explanation to it. – BDL Dec 12 '16 at 14:13