title: How to check if permission is granted by user at runtime on Android?
body: <p>I have created a simple android activity that acts as a dial.
  It has an edit text for the phone number and a call button
  Here is the code : (android 6.0 marshmallow)</p>
  <pre><code>public class Main2Activity extends AppCompatActivity {
  EditText num;
  Button call;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main2);
      num = (EditText) findViewById(R.id.num);
      call = (Button) findViewById(R.id.call);
      call.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              try {
                  // request permission if not granted
                  if (ActivityCompat.checkSelfPermission(Main2Activity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                      ActivityCompat.requestPermissions(Main2Activity.this, new String[]{Manifest.permission.CALL_PHONE}, 123);
                      // i suppose that the user has granted the permission
                      Intent in = new Intent(Intent.ACTION_CALL, Uri.parse(&quot;tel:&quot; + num.getText().toString()));
                      startActivity(in);
                   // if the permission is granted then ok
                  } else {
                      Intent in = new Intent(Intent.ACTION_CALL, Uri.parse(&quot;tel:&quot; + num.getText().toString()));
                      startActivity(in);
                  }
              }
              // catch the exception if I try to make a call and the permission is not granted
              catch (Exception e){
              }
          }
      });
  }
  </code></pre>
  <p>}</p>
  <p>When I run my app, I have these issues</p>
  <ul>
  <li><p>If I click on the call button and grant permission, the intent is not called until I click again</p>
  </li>
  <li><p>I don't know how to check if the permission was granted or not</p>
  </li>
  </ul>