【问题标题】:How to make phone calls programmatically in Android?如何在 Android 中以编程方式拨打电话?
【发布时间】:2014-04-30 22:29:29
【问题描述】:

我在 use_numbers_item_fragment.xml 中定义了以下布局:

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:id="@+id/call_linear_layout">

        <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:orientation="vertical">
            <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/useful_nums_item_name"/>

            <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/useful_nums_item_value"/>
        </LinearLayout>

       <ImageButton
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:src="@drawable/call"
                android:id="@+id/call_btn"
                android:onClick="callNumber"/>

    </LinearLayout>

我在一个名为 UNItemListFragment.java 的类中动态填充两个文本视图 在 onCreate 方法中:

public void onCreate(Bundle savedInstance) {
        super.onCreate(savedInstance);

        if (getArguments().containsKey(Constants.UNItem.GROUP_ID)) {

            simpleCursorAdapter = new SimpleCursorAdapter(getActivity(), R.layout.useful_numbers_item_fragment, null,
                    new String[]{Constants.UNItem.NAME, Constants.UNItem.VALUE},
                    new int[]{R.id.useful_nums_item_name, R.id.useful_nums_item_value}, 0);
            setListAdapter(simpleCursorAdapter);
            getLoaderManager().initLoader(0, getArguments(), this);

        }
    }

对于每个号码,如果我点击我想拨打电话的按钮 当用户点击按钮时调用 callNumber 方法:

public void callNumber(View view) {
        Intent callIntent = new Intent(Intent.ACTION_CALL);
        TextView unItemVal = (TextView) findViewById(R.id.useful_nums_item_value);

            String phoneNumber = unItemVal.getText().toString();
            callIntent.setData(Uri.parse("tel:" + phoneNumber));
            startActivity(callIntent);
    }

当我单击列表中的第一个按钮时可以,但是当我单击其他按钮时 它继续调用第一行中定义的号码...

知道如何解决这个问题吗?

【问题讨论】:

    标签: java android android-intent phone-call


    【解决方案1】:

    问题在于这一行:

    TextView unItemVal = (TextView) findViewById(R.id.useful_nums_item_value);
    

    在活动上执行,因此findViewById 将始终返回具有该 id 的第一项,这可能是列表中的第一项。

    解决此问题的最佳方法是覆盖适配器并将包含电话号码的标签添加到视图中。解决此问题的 快速 方法是在视图层次结构中进行标记,如下所示:

    public void callNumber(View view) {
        if( view != null ) { // view is the button tapped
            View parent = view.getParent(); // this should be the LinearLayout
            if( parent instanceof LinearLayout ) {
                TextView unItemVal = (TextView) ((LinearLayout)parent).findViewById(R.id.useful_nums_item_value);
                if( unItemVal != null ) {
                    Intent callIntent = new Intent(Intent.ACTION_CALL);
                    String phoneNumber = unItemVal.getText().toString();
                    callIntent.setData(Uri.parse("tel:" + phoneNumber));
                    startActivity(callIntent);
                }
            }
        }
    }
    

    这将找到被点击按钮的父级,然后找到包含ViewGroup 中数字的文本视图。

    【讨论】:

      【解决方案2】:

      使用findViewById() 将返回具有指定ID 的活动或片段中的first 视图。如果这是一个ListView,它将对应第一行。

      有很多方法可以解决这个问题。最快的(但肯定不是最漂亮的,因为它取决于布局)是相对于包含列表项的 LinearLayout 使用 findViewById()。假设 view 是 ImageButton,它会是这样的:

      ((View)view.getParent()).findViewById(R.id.useful_nums_item_value)
      

      更优雅的解决方案是在适配器的getView() 中设置一个标签,其中包含您需要的数据(在这种情况下,是要拨打的电话号码)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-12
        相关资源
        最近更新 更多