Android - 概述

Android - 电话

Android提供内置的电话应用程序,在某些情况下,我们可能需要通过我们的应用程序拨打电话.这可以通过使用隐式Intent和适当的操作来轻松完成.此外,我们可以使用PhoneStateListener和TelephonyManager类来监视设备上某些电话状态的变化.

本章列出了创建应用程序的所有简单步骤,曾经打过电话.您可以通过调用Android的内置电话功能来使用Android Intent拨打电话.以下部分解释了调用所需的Intent对象的不同部分.

意图对象 - 拨打电话的动作

您将使用 ACTION_CALL 触发Android设备中可用的内置电话功能的操作.以下是使用ACTION_CALL动作创建意图的简单语法

 
 Intent phoneIntent = new Intent(Intent.ACTION_CALL);

您可以使用 ACTION_DIAL 操作代替ACTION_CALL,在这种情况下,您可以选择在拨打电话前修改硬编码的电话号码而不是直接打电话.

意图对象 - 拨打电话的数据/类型

拨打给定号码的电话91- 000-000-0000,您需要使用setData()方法将 tel:指定为URI,如下所示 :

 
 phoneIntent.setData(Uri.parse("tel:91-000-000-0000"));

有趣的是,要拨打电话,您无需指定任何额外的数据或数据类型.

示例

以下示例向您展示如何使用Android Intent拨打给定的手机号码.

进行实验在这个例子中,您将需要配备最新Android操作系统的实际移动设备,否则您将不得不与可能无法使用的模拟器斗争.
Step描述
1您将使用Android studio IDE创建Android应用程序并将其命名为我的应用程序在包 com.example.saira_000.myapplication 下.
2修改 src/MainActivity.java 文件并添加所需的代码以便进行通话.
3修改布局XML文件 res/layout/activity_main.xml 如果需要,添加任何GUI组件.我正在添加一个简单的按钮来拨打91-000-000-0000号码
4无需定义默认字符串常量.Android studio负责默认常量.
5修改 AndroidManifest.xml ,如下所示
6运行应用程序以启动Android模拟器并验证应用程序中所做更改的结果.

以下是修改后的主要活动文件的内容 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的内容 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 的内容来定义两个新的常量 :

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="app_name">My Application</string>
</resources>

以下是 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>

让我们尝试运行您的我的应用程序应用程序.我假设您已将实际的Android移动设备与计算机相关联.要从Android工作室运行应用程序,请打开项目的一个活动文件,然后单击运行Eclipse Run Icon icon从工具栏中选择您的移动设备作为选项,然后检查您的移动设备,它将显示以下屏幕 :

Android移动呼叫屏幕

现在使用呼叫按钮拨打电话,如下所示 :

Android Mobile Call Progress