title: Best Way to get Device IMEI number android java programmatically with onRequestPermissionsResult
body: <hr>
  
  <hr>
  
  <p>********************************best way i found:**************************************
  ***************************that's workd successfully***********************************
  *****************************just copy below codes*************************************</p>
  
  <p>Manifest:</p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.READ_PHONE_STATE" /&gt;
  </code></pre>
  
  <p>JAVA:
  in onCreate:</p>
  
  <pre><code>String deviceIMEI;
  
  CheckPermissionAndStartIntent();
  </code></pre>
  
  <p>in root:</p>
  
  <pre><code>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 &gt; 0 &amp;&amp; grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                  doSomthing();
              } else if (grantResults.length &gt; 0 &amp;&amp; grantResults[0] == PackageManager.PERMISSION_DENIED) {
                  //SEY SOMTHING LIKE YOU CANT ACCESS WITHOUT PERMISSION
                  //you can show something to user and open setting -&gt; apps -&gt; youApp -&gt; 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;
  }
  </code></pre>