Android gets IMEI value of mobile phone

During the production of mobile phones, each mobile phone has a unique ID, and international mobile equipment identity (IMEI) is adopted internationally. IMEI is an "electronic serial number" composed of 15 digits. It corresponds to each mobile phone one by one, and the code is unique in the world. After assembly, each mobile phone will be given a unique set of numbers, which will be recorded by manufacturers from production to delivery.

Enter * ා06ාin the mobile phone to see the IMEI code of this phone. You can also find the IMEI code on the phone box.

In the development of mobile applications, using IMEI to do identity authentication is a common technology.

In the Android SDK, the android.phone.telephonymanager class provides related operation and management of mobile device information.

Add in code

private void setPhoneStateManifest(){
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            // toast("dynamic access required");
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_PHONE_STATE);
        }else{
            // toast("no dynamic access required");
            TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
            String IMEI = tm.getDeviceId();
            mPhoneState.setText(IMEI);
            Log.i(TAG,"IMEI:" + IMEI);
        }
    }


@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if (requestCode == REQUEST_PHONE_STATE && grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
        String IMEI = tm.getDeviceId();
        mPhoneState.setText(IMEI);
        Log.i(TAG,"IMEI:" + IMEI);
    }
}

Don't forget permissions:

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

Keywords: Mobile Android SDK

Added by nepton on Sun, 05 Jan 2020 22:36:28 +0200