Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

android

imei



********************************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;
}
like image 201
Shahriyar Aghajani Avatar asked Feb 01 '18 06:02

Shahriyar Aghajani


People also ask

Can we get IMEI number in Android programmatically?

The method getDeviceId() of TelephonyManager returns the unique device ID, for example, the IMEI for GSM and the MEID or ESN for CDMA phones. Return null if device ID is not available. Permission Required READ_PHONE_STATE in manifest file.

Can Android 10 get programmatically IMEI number?

Starting in Android 10 (API level 29), all the APIs getDeviceId() , getImei() (IMEI for GSM) or getMeid() (MEID for CDMA) are restricted to third-party apps and cannot access the device's IMEI and serial number (non-resettable identifiers). This is to guard the device identifiers.

How can I get IMEI number from ADB?

You can check and save the IMEI number of your device by dialing *#06# in your dialer app and taking a screenshot of it. Alternatively, you can use this ADB command on your PC. It will display the IMEI number in the Command Prompt, which you can copy and paste wherever you want for safekeeping.

How can I find my serial number in Android programmatically?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken text view to show serial number.


2 Answers

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";
}
like image 162
Saurabh Vadhva Avatar answered Oct 29 '22 22:10

Saurabh Vadhva


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>
like image 20
kamal verma Avatar answered Oct 30 '22 00:10

kamal verma