javaandroidandroid-intentandroid-dialer

Getting RESULT_CANCELED for Dialer Intent


I am trying to get result for dialer Intent using startActivityForResult()

Below is my code for Dialer Intent.

        button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_DIAL);
            intent.setData(Uri.parse("tel:123456789"));
            startActivityForResult(intent, 1234);
           }
        });

        @Override
        protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
          if(requestCode == 1234){

           if (resultCode == Activity.RESULT_OK){
             Toast.makeText(getApplicationContext(), "result ok", Toast.LENGTH_LONG).show();
           }else if (resultCode == Activity.RESULT_CANCELED){
               Toast.makeText(getApplicationContext(), "Result Cancelled", Toast.LENGTH_LONG).show();
           }
          }

       }

whenever I am returning to my activity, Result Cancelled Toast is triggering.

Thanks in advance.


Solution

  • From doc:

    ACTION_DIAL

    public static final String ACTION_DIAL
    

    You only have the ACTION. If you want to call a number from your application then you just have to put these lines of code into the onClick() method and can get what you want:

    Intent intent = new Intent(Intent.ACTION_DIAL);
    intent.setData(Uri.parse("tel:123456789"));
    startActivity(intent); // no need to use startActivityResult(intent,1234)
    

    Here, If an ACTION_DIAL input is nothing, an empty dialer is started; else getData() is URI of a phone number to be dialed or a tel: <yourURI> of an explicit phone number. Additionally, there is no "Output" of RESULT_OK or RESULT_CANCELED, Cause, the startActivityResult() doesn't mean anything to ACTION_DIAL but startActivity(intent). Hope it helps.