Get IMEI on Android without requesting permissions

2

I have an app where I identify each device with the number IMEI ,

The problem is that to request this number, androiod shows a window to the user indicating that if I grant permission to access the calls and the state of the phone. So many users say no

This is a problem because without this code I can not identify devices or associate a device with a user.

Is there any way to request the imei without permissions? or to request another unique device code?

The code I use is the following:

   public void getImeiFunc() {
    int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE);
    if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_READ_PHONE_STATE);
    }
    else {
        TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        String imei;
        if (Build.VERSION.SDK_INT >= 26) {
            imei = telephonyManager.getImei(0);
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                imei = telephonyManager.getDeviceId(0);
            } else {
                imei = telephonyManager.getDeviceId();
            }
        }
    }
}
    
asked by Alberto Mier 29.11.2017 в 08:21
source

1 answer

0

You must remember that for devices with OS 6.0 or higher it is necessary to manually require some permissions that are considered "dangerous", among these permits is the permission: READ_PHONE_STATE .

One way to avoid requiring permissions is to define a targetSdkVersion less than 23 , but in In the future, your application will require updating the Libraries to its most recent versions and therefore you will have to define a larger targetSdkVersion.

It is very important to define that you can not avoid requesting these permissions, if you do it simply your application will not work correctly . One way that I recommend you is to open a dialog before requesting permission to suggest to the user to accept the following permissions in order for your application to work correctly.

This is a method to obtain the IMEI of a device in all operating system versions:

  public String obtenerImei(){

        if(Build.VERSION.SDK_INT  < Build.VERSION_CODES.M){
            //Menores a Android 6.0
            String imei= getIMEI();
            return imei;
        } else {
            // Mayores a Android 6.0
            String imei="";
            if (checkSelfPermission(Manifest.permission.READ_PHONE_STATE)
                    != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE},
                        MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);
                imei="";
            } else {
                imei= getIMEI();
            }

            return imei;

        }
    }

    private String getIMEI() {

        TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        String imei =tm.getDeviceId(); 
        return imei;

    }
    
answered by 29.11.2017 / 16:58
source