Best Way to get Device IMEI number android java programmatically with onRequestPermissionsResult [duplicate]
Asked Answered
A

3

5


********************************best way i found:************************************** ***************************that's workd successfully*********************************** *****************************just copy below codes*************************************

Manifest:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

JAVA: in onCreate:

String deviceIMEI;

CheckPermissionAndStartIntent();

in root:

private void CheckPermissionAndStartIntent() {
    if (ContextCompat.checkSelfPermission(SplashActivity.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED
            || ContextCompat.checkSelfPermission(SplashActivity.this, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_DENIED) {
        ActivityCompat.requestPermissions(SplashActivity.this, new String[]{Manifest.permission.READ_PHONE_STATE}, 1);
        //SEY SOMTHING LIKE YOU CANT ACCESS WITHOUT PERMISSION
    } else {
        doSomthing();
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
    switch (requestCode) {
        case 1: {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                doSomthing();
            } else if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_DENIED) {
                //SEY SOMTHING LIKE YOU CANT ACCESS WITHOUT PERMISSION
                //you can show something to user and open setting -> apps -> youApp -> permission
                // or unComment below code to show permissionRequest Again
                //ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_PHONE_STATE}, 1);
            }
        }
    }
}


doSomthing() {
    deviceIMEI = getDeviceIMEI(MainActivity.this);
    //andGoToYourNextStep
}

@SuppressLint("HardwareIds")
public static String getDeviceIMEI(Activity activity) {

    String deviceUniqueIdentifier = null;
    TelephonyManager tm = (TelephonyManager) activity.getSystemService(Context.TELEPHONY_SERVICE);
    if (null != tm) {
        if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED)
            ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_PHONE_STATE}, 1);
        else
            deviceUniqueIdentifier = tm.getDeviceId();
        if (null == deviceUniqueIdentifier || 0 == deviceUniqueIdentifier.length())
            deviceUniqueIdentifier = "0";
    }
    return deviceUniqueIdentifier;
}
Assyriology answered 1/2, 2018 at 6:24 Comment(1)
i saw thats topic but i can't find my resultAssyriology
A
9

Please use this

manifest file

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

In Java File

 public static String getUniqueIMEIId(Context context) {
    try {
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return "";
        }
        String imei = telephonyManager.getDeviceId();
        Log.e("imei", "=" + imei);
        if (imei != null && !imei.isEmpty()) {
            return imei;
        } else {
            return android.os.Build.SERIAL;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "not_found";
}
Alanalana answered 1/2, 2018 at 6:37 Comment(13)
thanQ but thats return null --> ( " " )Assyriology
are you check in Android Device?Alanalana
yes my device is real not virtual device :'(Assyriology
ok. are you allow the runtime permission in your application.?Alanalana
because its working fine from my side.Alanalana
yes i allowed permission,Assyriology
IMEI = getUniqueIMEIId(DashboardActivity.this); i'm using your class like this, is it true?Assyriology
Right..please share the error message.Alanalana
that's work and dont give error, but return null in my string, and dont return imei numberAssyriology
I'm update my code.Please check now.Alanalana
Call requires permission which may be rejected by user: code should explicitly check to see if permission is available (with checkPermission) or explicitly handle a potential SecurityExceptionThis check scans through your code and libraries and looks at the APIs being used, and checks this against the set of permissions required to access those APIs. If the code using those APIs is called at runtime, then the program will crash. Furthermore, for permissions that are revocable (with targetSdkVersion 23), client code must also be prepared to handle the calls throwing an exception if the user...Assyriology
Please replace "android.os.Build.SERIAL" to "IMEI number not found". then check and let me know.Alanalana
use: String imei = telephonyManager.getImei(); String serial = android.os.Build.getSerial(); for new versions android sdks (for ex. API 27 which I am using currently)Botryoidal
U
1

Try this

TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling

        return;
    }
    String imei = telephonyManager.getDeviceId();

Add Permission in Manifest.xml

<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
Umbilical answered 1/2, 2018 at 6:32 Comment(0)
U
0
public  String getIMEI(final Context context) {
    final TelephonyManager ts =
    (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    return ts.getDeviceId()) ;
}

make sure you have Manifest.permission.READ_PHONE_STATE Permission

Umpire answered 1/2, 2018 at 6:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.