【问题标题】:How to open dialer on phone with a selected number in android [duplicate]如何在android中使用所选号码在手机上打开拨号器[重复]
【发布时间】:2016-07-18 11:15:58
【问题描述】:

我制作了一个使用 Google Places API 的应用程序。一旦我点击一个地方,它就会将公司名称、地址和号码返回给 MainActivity。当我点击号码时,我可以打开拨号器,但似乎无法将 MainActivity 返回的号码输入拨号器。关于我如何去做这件事的任何想法。谢谢

我的代码如下

主活动

    private TextView mName;
    private TextView mAddress;
    private TextView mNumber;
  private static final LatLngBounds Sligo = new LatLngBounds(
            new LatLng(54.27, -8.47), new LatLng(54.27, -8.47));
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_main);
        mName = (TextView) findViewById(R.id.textView);
        mAddress = (TextView) findViewById(R.id.textView2);
        mAttributions = (TextView) findViewById(R.id.textView3);
        mNumber = (TextView) findViewById(R.id.textView4);
        Button pickerButton = (Button) findViewById(R.id.pickerButton);

        pickerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try {
                    PlacePicker.IntentBuilder intentBuilder =
                            new PlacePicker.IntentBuilder();
                    intentBuilder.setLatLngBounds(Sligo);


                    List<Integer> filterTypes = new ArrayList<Integer>();
                    filterTypes.add(Place.TYPE_CAR_REPAIR);


                    Intent intent = intentBuilder.build(MainActivity.this);
                    startActivityForResult(intent, PLACE_PICKER_REQUEST );
                } catch (GooglePlayServicesRepairableException
                        | GooglePlayServicesNotAvailableException e) {
                    e.printStackTrace();
                }
            }
        });
    @Override
    protected void onActivityResult(int requestCode,
                                    int resultCode, Intent data) {

        if (requestCode == PLACE_PICKER_REQUEST
                && resultCode == Activity.RESULT_OK) {

            final Place place = PlacePicker.getPlace(this, data);
            final CharSequence name = place.getName();
            final CharSequence address = place.getAddress();
            final CharSequence formatted_phone_number = place.getPhoneNumber();

            //    final CharSequence car = place.TYPE_CAR_REPAIR();
            //public abstract List<Integer> getTypeFilter(place.TYPE_CAR_REPAIR);
            String attributions = (String) place.getAttributions();
            if (attributions == null) {
                attributions = "";
            }

            mName.setText(name);
            mAddress.setText(address);
            mAttributions.setText(Html.fromHtml(attributions));
            mNumber.setText(formatted_phone_number);



        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }

onClick 事件

  public void onClickNumber(View arg)
    {
        Intent intent = new Intent(Intent.ACTION_DIAL);

        startActivity(intent);

    }

数字在屏幕底部

【问题讨论】:

  • @MikeM。你看到的事情是它实际上可以是任何数字,这只是一个例子,所以我不能设置一个具体的数字。我找不到将我带到带有我选择的号码的拨号器的答案
  • 使用formatted_phone_number你正在进入onActivityResult()

标签: java android android-studio google-places-api


【解决方案1】:

试试这个。

String number = "494498498";
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:" +number));
startActivity(intent);

【讨论】:

  • 你看,它实际上可以是任何数字,这只是一个例子,所以我不能设置一个具体的数字
  • @CraigGallagher 你不能将你的号码传递给数字字符串。这样做有什么问题
  • 您可以设置任何数字。就像从 TextView 中获取您的号码并将其设置为 intent.setData(Uri.parse("tel:"+ yourTextView.getText().toString()));
【解决方案2】:

试试这个

String phone = mNumber.getText().toString();
Intent phoneIntent = new Intent(Intent.ACTION_DIAL, Uri.fromParts(
"tel", phone, null));
startActivity(phoneIntent);

【讨论】:

  • 这很有效,谢谢
  • 总是 wc @CraigGallagher
猜你喜欢
  • 2023-04-10
  • 2012-05-25
  • 2020-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多