如何在Android

时间:2016-03-30 12:35:31

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

我制作了一个使用Google Places API的应用程序。点击一个地方后,它会返回公司名称,地址和MainActivity的编号。当我点击号码时,我可以打开拨号器,但我似乎无法将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);

    }

号码位于屏幕底部 enter image description here

2 个答案:

答案 0 :(得分:14)

试试这个

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

答案 1 :(得分:2)

试试这个。

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