0

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.

Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
  • Why do you need the `onActivityResult()` method, can you please explain what are your expected output? – A S M Sayem Oct 12 '19 at 08:55
  • I need to know whether I called that number using intent. But it simply returning with RESULT_CANCELED result code. – vijay raghavan Oct 12 '19 at 08:59
  • **"I need to know whether I called that number using intent"** I don't think you will need `onActivityResult()` to check this. You can check that on your emulator/device. Isn't it? – A S M Sayem Oct 12 '19 at 09:10
  • Thanks @Saadat, any other way to do this. Also why am I getting RESULT_CANCELED instead of RESULT_OK. – vijay raghavan Oct 12 '19 at 09:17

2 Answers2

0

why am I getting RESULT_CANCELED instead of RESULT_OK.

ACTION_DIAL does not return a result. If you read the documentation for ACTION_DIAL, you will see "Output: nothing". Hence, you will usually get RESULT_CANCELED. Only activities designed for use with startActivityForResult() will return a result code.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

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.

A S M Sayem
  • 2,010
  • 2
  • 21
  • 28