Monday, May 27, 2019

How to get Device IME Number in Android Programmatically


Need to add require permission in AndroidMenifest.xml

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

Now need to Allow user permission at run time in your AppCompatActivity

public void FindIME() {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE)
                != PackageManager.PERMISSION_GRANTED) {
            // your READ_PHONE_STATE permission yet not granted.
            DevicePermission();
        } else {
            // your READ_PHONE_STATE permission is already been granted.
            ActivityCompat.requestPermissions(FlashScreen.this, new String[]{Manifest.permission.READ_PHONE_STATE}, PERMISSION_READ_STATE);
            GetIME();
        }
    }

//Here we need to ask for user  permission

private static final int MY_PERMISSIONS_REQUEST_READ_PHONE_STATE = 0;
private void DevicePermission() {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.READ_PHONE_STATE)) {
   
       new AlertDialog.Builder(FlashScreen.this)
                    .setTitle("Permission Request")
                    .setMessage(getString(R.string.permission_read_phone_state_rationale))
                    .setCancelable(false)
                    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            //re-request
                            ActivityCompat.requestPermissions(FlashScreen.this,
                                    new String[]{Manifest.permission.READ_PHONE_STATE},
                                    MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);
                        }
                    })
                    .setIcon(R.drawable.ic_login)
                    .show();
        } else {
            // READ_PHONE_STATE permission has not been granted yet. Request it directly.
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE},
                    MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);
        }
 }

public void GetIME() {
        String deviceUniqueIdentifier = null;
        TelephonyManager tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
        if (null != tm) {
            deviceUniqueIdentifier = tm.getDeviceId();
        }
        if (null == deviceUniqueIdentifier || 0 == deviceUniqueIdentifier.length()) {
            deviceUniqueIdentifier = Settings.Secure.getString(this.getContentResolver(), Settings.Secure.ANDROID_ID);
        }

        IME_Device = deviceUniqueIdentifier;
        Log.i("Device Ime", "Value--------->" + IME_Device);
     
    }

@Override
 public void onRequestPermissionsResult(int requestCode,
 String permissions[], int[] grantResults) {
        switch (requestCode) {

            case MY_PERMISSIONS_REQUEST_READ_PHONE_STATE: {

                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // permission granted!
                 
                    GetIME();
                } else {
                    // permission denied
                    Toast.makeText(mContext, "Need to allow Read Phone state ..", Toast.LENGTH_LONG).show();
                }
            }
            break;

            case PERMISSION_READ_STATE: {
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // permission granted!
                 
                } else {
                    // permission denied
                    Toast.makeText(mContext, "Need to allow Read Phone state ..", Toast.LENGTH_LONG).show();
                }

            }
            break;
        }

    }

No comments:

Post a Comment