Android 简明教程
Android - Phone Calls
Android 提供内置应用程序用于拨打电话,在某些情况下,我们可能需要通过我们的应用程序拨打电话。这可以通过使用具有适当操作的隐式意图轻松完成。此外,我们可以使用 PhoneStateListener 和 TelephonyManager 类,以便监控设备上某些电话状态的变化。
Android provides Built-in applications for phone calls, in some occasions we may need to make a phone call through our application. This could easily be done by using implicit Intent with appropriate actions. Also, we can use PhoneStateListener and TelephonyManager classes, in order to monitor the changes in some telephony states on the device.
本章列出了创建可用于拨打电话的应用程序所需的所有简单步骤。你可以使用 Android 意图通过调用 Android 的内置拨打电话功能来拨打电话。以下部分说明进行拨号所需的意图对象的各个部分。
This chapter lists down all the simple steps to create an application which can be used to make a Phone Call. You can use Android Intent to make phone call by calling built-in Phone Call functionality of the Android. Following section explains different parts of our Intent object required to make a call.
Intent Object - Action to make Phone Call
你需要使用 ACTION_CALL 操作来触发 Android 设备中可用的内置拨号功能。以下是要使用 ACTION_CALL 操作创建意图的简单语法:
You will use ACTION_CALL action to trigger built-in phone call functionality available in Android device. Following is simple syntax to create an intent with ACTION_CALL action
Intent phoneIntent = new Intent(Intent.ACTION_CALL);
你可以使用 ACTION_DIAL 操作代替 ACTION_CALL,在这种情况下,在进行直接拨号之前,你将可以选择修改硬编码的电话号码。
You can use ACTION_DIAL action instead of ACTION_CALL, in that case you will have option to modify hardcoded phone number before making a call instead of making a direct call.
Intent Object - Data/Type to make Phone Call
要拨打给定的号码 91-000-000-0000,你需要使用 setData() 方法将 tel: 指定为 URI,如下所示:
To make a phone call at a given number 91-000-000-0000, you need to specify tel: as URI using setData() method as follows −
phoneIntent.setData(Uri.parse("tel:91-000-000-0000"));
有趣的是,为了拨打电话,不需要指定任何额外的 data 或数据类型。
The interesting point is that, to make a phone call, you do not need to specify any extra data or data type.
Example
以下示例向你实际展示如何使用 Android 意图拨打给定的移动电话号码。
Following example shows you in practical how to use Android Intent to make phone call to the given mobile number.
Step |
Description |
1 |
You will use Android studio IDE to create an Android application and name it as My Application under a package com.example.saira_000.myapplication. |
2 |
Modify src/MainActivity.java file and add required code to take care of making a call. |
3 |
Modify layout XML file res/layout/activity_main.xml add any GUI component if required. I’m adding a simple button to Call 91-000-000-0000 number |
4 |
No need to define default string constants.Android studio takes care of default constants. |
5 |
Modify AndroidManifest.xml as shown below |
6 |
Run the application to launch Android emulator and verify the result of the changes done in the application. |
以下是修改的主活动文件 src/MainActivity.java 的内容。
Following is the content of the modified main activity file src/MainActivity.java.
package com.example.saira_000.myapplication;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.buttonCall);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:0377778888"));
if (ActivityCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
return;
}
startActivity(callIntent);
}
});
}
}
以下是 res/layout/activity_main.xml 文件的内容——
Following will be the content of res/layout/activity_main.xml file −
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/buttonCall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="call 0377778888" />
</LinearLayout>
以下是 res/values/strings.xml 的内容,用于定义两个新常量——
Following will be the content of res/values/strings.xml to define two new constants −
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">My Application</string>
</resources>
以下是 AndroidManifest.xml 的默认内容−
Following is the default content of AndroidManifest.xml −
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.saira_000.myapplication" >
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.saira_000.myapplication.MainActivity"
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>
</manifest>
让我们尝试运行你的 My Application 应用程序。我假设已将你的实际 Android 移动设备与计算机连接。要从 Android Studio 运行该应用程序,请打开项目中的一个活动文件,并单击工具栏中的“运行”图标。选择你的移动设备作为选项,然后查看移动设备,它将显示以下屏幕:
Let’s try to run your My Application application. I assume you have connected your actual Android Mobile device with your computer. To run the app from Android studio, open one of your project’s activity files and click Run icon from the toolbar.Select your mobile device as an option and then check your mobile device which will display following screen −
现在,使用“通话”按钮进行拨号,如下所示:
Now use *Call * button to make phone call as shown below −