web-dev-qa-db-fra.com

Meilleure façon d'obtenir un numéro IMEI de périphérique Android Java par programme avec onRequestPermissionsResult



********************************* meilleur moyen que j'ai trouvé: ************* ************************* ********************* ***** travaillée avec succès *************************************** ************************* suffit de copier les codes ci-dessous ********************* ****************

Manifeste:

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

Java: Dans onCreate:

String deviceIMEI;

CheckPermissionAndStartIntent();

en racine:

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;
}
3
Shahriyar Aghajani

S'il vous plaît utiliser cette

fichier manifeste

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

En fichier Java

 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";
}
4
Saurabh Vadhva

Essaye ça 

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();

Ajouter une autorisation dans Manifest.xml

<uses-permission Android:name="Android.permission.READ_PHONE_STATE"></uses-permission>
1
kamal verma
public  String getIMEI(final Context context) {
    final TelephonyManager ts =
    (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    return ts.getDeviceId()) ;
}

assurez-vous de disposer de l'autorisation Manifest.permission.READ_PHONE_STATE

0
sanemars