How to Get IMEI on Android 5 in firemonkey?

3.4k views Asked by At

I try to get IMEI on android device and i can do this on android 4.2 successfully

but my written code does not work on android 5 and later

how do get IMEI on android 5 in firemonkey?

1

There are 1 answers

1
aleroot On

Here is a code snippet which gets the Android device IMEI for you using the Android JNI :

uses
Androidapi.JNI.Telephony, Androidapi.JNI.Provider ,
Androidapi.JNIBridge, Androidapi.JNI.GraphicsContentViewText ,
Androidapi.JNI.JavaTypes,FMX.Helpers.Android;

event :

procedure TForm1.btnCapturarIMEIClick(Sender: TObject);
var
  obj: JObject;
  tm: JTelephonyManager;
  IMEI: String;
begin
  obj := SharedActivityContext.getSystemService(TJContext.JavaClass.TELEPHONY_SERVICE);
  if obj <> nil then
  begin
    tm := TJTelephonyManager.Wrap( (obj as ILocalObject).GetObjectID );
    if tm <> nil then
      IMEI := JStringToString(tm.getDeviceId);
  end;
  if IMEI = '' then
      IMEI := JStringToString(TJSettings_Secure.JavaClass.getString(                        SharedActivity.getContentResolver,                        TJSettings_Secure.JavaClass.ANDROID_ID));

  ShowMessage('IMEI :' + #13 + IMEI);

end;