Android拨打电话功能实例详解

2019-12-10 19:07:30王振洲

res/layout/main.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://www.easck.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" >
  <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />
  <Button
    android:id="@+id/button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
  <EditText
    android:id="@+id/et"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
</LinearLayout>

AndroidManifest.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://www.easck.com/apk/res/android"
  package="com.my.a01"
  android:versionCode="1"
  android:versionName="1.0" >
  <uses-sdk android:minSdkVersion="10" />
  <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
      android:name=".A01Activity"
      android:label="@string/app_name" >
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
  </application>
  <uses-permission android:name="android.permission.CALL_PHONE">
  </uses-permission>
</manifest>

通过Button来拨打电话,在onClick()方法中,自定义一个Intent,传入ACTION_CALL与Uri.parse(),传入的Uri数据中电话的前缀为“tel:”。

注意要添加拨打电话的权限android.permission.CALL_PHONE