简体   繁体   English

为拨号器意图获取 RESULT_CANCELED

[英]Getting RESULT_CANCELED for Dialer Intent

I am trying to get result for dialer Intent using startActivityForResult()我正在尝试使用startActivityForResult()获取拨号器 Intent 的结果

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.每当我返回我的活动时,都会触发 Result Canceled Toast。

Thanks in advance.提前致谢。

why am I getting RESULT_CANCELED instead of RESULT_OK.为什么我得到 RESULT_CANCELED 而不是 RESULT_OK。

ACTION_DIAL does not return a result. ACTION_DIAL不返回结果。 If you read the documentation for ACTION_DIAL , you will see "Output: nothing".如果您阅读ACTION_DIAL的文档,您将看到“输出:无”。 Hence, you will usually get RESULT_CANCELED .因此,您通常会得到RESULT_CANCELED Only activities designed for use with startActivityForResult() will return a result code.只有设计用于startActivityForResult()的活动才会返回结果代码。

From doc :来自文档

ACTION_DIAL ACTION_DIAL

public static final String ACTION_DIAL

You only have the ACTION .你只有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:如果你想从你的应用程序中调用一个号码,那么你只需将这些代码行放入onClick()方法中就可以得到你想要的:

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;此处,如果ACTION_DIAL输入为空,则启动空拨号器; else getData() is URI of a phone number to be dialed or a tel: <yourURI> of an explicit phone number.否则getData()是要拨打的电话号码的 URI 或明确电话号码的tel: <yourURI> Additionally, there is no "Output" of RESULT_OK or RESULT_CANCELED , Cause, the startActivityResult() doesn't mean anything to ACTION_DIAL but startActivity(intent) .此外,没有RESULT_OKRESULT_CANCELED的“输出”,因为startActivityResult()ACTION_DIAL没有任何意义,而是startActivity(intent) Hope it helps.希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM