您好!欢迎来到源码码网

Android实现电话拨打功能

  • 源码教程
  • 来源:源码码网
  • 编辑:admin
  • 时间:2021-01-13 17:12
  • 阅读:560

Android开发借助于 Intent 可以轻松实现拨打电话的应用程序。只需声明一个拨号的 Intent 对象,并使用 startActivity() 方法启动即可。

创建 Intent 对象的代码为 Intent intent = new Intent(action,uri),其中 URI 是要拨叫的号码数据,通过 Uri.parse() 方法把“tel:1234”格式的字符串转换为 URI。

而 Action 有两种使用方式:

  • Intent.Action_CALL。是直接进行呼叫的方式,这种方式需要应用程序具有 android.permission.CALL_PHONE 权限。

  • Intent.Action_DIAL。不是不直接进行呼叫,而是启动 Android 系统的拨号应用程序,然后由用户进行拨号。这种方式不需要任何权限的设置。


实例 phoneDemo 演示了使用 Intent.Action_CALL 方式进行拨号的过程,运行效果如图 1 所示。

使用Intent.Action_CALL方式拨号

图 1  使用Intent.Action_CALL方式拨号

实例 phoneDemo 中 main.xml 的代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>

  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  3. android:layout_width="fill_parent"

  4. android:layout_height="fill_parent"

  5. android:orientation="vertical">


  6. <EditText

  7. android:id="@+id/edittext"

  8. android:layout_width="fill_parent"

  9. android:layout_height="wrap_content"

  10. android:layout_marginLeft="40dp"

  11. android:layout_marginTop="30dp" />


  12. <Button

  13. android:id="@+id/button"

  14. android:layout_width="wrap_content"

  15. android:layout_height="wrap_content"

  16. android:layout_marginLeft="80dp"

  17. android:layout_marginTop="40dp"

  18. android:text="拨打电话" />


  19. </LinearLayout>

实例 phoneDemo 中 AndroidManifest.xml 的代码如下:


  1. <?xml version="1.0" encoding="utf-8"?>

  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"

  3. package="introduction.android.phonedemo">


  4. <uses-permission android:name="android.permission.CALL_PHONE" />

  5. <application

  6. android:allowBackup="true"

  7. android:icon="@mipmap/ic_launcher"

  8. android:label="@string/app_name"

  9. android:roundIcon="@mipmap/ic_launcher_round"

  10. android:supportsRtl="true"

  11. android:theme="@style/AppTheme">

  12. <activity android:name=".MainActivity">

  13. <intent-filter>

  14. <action android:name="android.intent.action.MAIN" />

  15. <category android:name="android.intent.category.LAUNCHER" />

  16. </intent-filter>

  17. </activity>

  18. </application>

  19. </manifest>

实例 phoneDemo 中 PhoneDemoActivity.java 的具体实现代码如下:


  1. package introduction.android.phonedemo;


  2. import android.app.Activity;

  3. import android.content.Intent;

  4. import android.net.Uri;

  5. import android.os.Bundle;

  6. import android.view.View;


  7. import android.view.View.OnClickListener;

  8. import android.widget.Button;

  9. import android.widget.EditText;


  10. public class MainActivity extends Activity {


  11. /**

  12.     * Called when the activity is first created.

  13.     */

  14. private Button button;

  15. private EditText edittext;


  16. @Override

  17. public void onCreate(Bundle savedInstanceState) {

  18. super.onCreate(savedInstanceState);

  19. setContentView(R.layout.activity_main);

  20. button = (Button) findViewById(R.id.button);

  21. button.setOnClickListener(new buttonListener());

  22. }


  23. class buttonListener implements OnClickListener {

  24. @Override

  25. public void onClick(View v) {

  26. // TODO Auto-generated method stub

  27. edittext = (EditText) findViewById(R.id.edittext);

  28. String number = edittext.getText().toString();

  29. Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + number));

  30. //startActivity(intent);

  31. }


  32. }


  33. }

其中:

Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + number));
startActivity(intent);

通过 Intent.ACTION_CALL 建立了一个进行拨号的 Intent 请求,并使用 startActivity 直接启动 Android 系统的拨号程序进行呼叫。

若在实例 PhoneDemo 中,将 PhoneDemoActivity.java 中的代码:

Intent intent=new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+number));

修改为:

Intent intent=new Intent(Intent.ACTION_DIAL,Uri.parse("tel:"+number));

最后,单击“拨打电话”按钮后不再直接呼叫,而是只运行 Android 系统默认的拨号程序,用户还拥有进一步决定下一步操作的权限,运行效果如图 2 所示。

拨打电话

图 2  拨打电话

特别声明:
1、如无特殊说明,内容均为本站原创发布,转载请注明出处;
2、部分转载文章已注明出处,转载目的为学习和交流,如有侵犯,请联系客服删除;
3、编辑非《源码码网》的文章均由用户编辑发布,不代表本站立场,如涉及侵犯,请联系删除;
全部评论(0)
推荐阅读
  • 常用测试压力工具使用介绍
  • 常用测试压力工具使用介绍
  • ab 是 ApacheBench 工具的缩写,它是一个HTTP压力测试工具。让我详细说明如何测试:1. 安装ApacheBenchWindows系统:方法一:安装XAMPP或WAMP(自带ab)下载地址:https://www.apachefriends.org/zh_cn/index.html安装后,ab工具在:C:xamppapacheinab.exe方法二:使
  • 开发工具
  • 来源:源码码网
  • 编辑:源码码网
  • 时间:2026-01-13 20:27
  • 阅读:59
  • 工程项目一体化自动管理软件解决方案
  • 工程项目一体化自动管理软件解决方案
  • 1.项目概述1.1项目背景在工程建设行业数字化转型浪潮下,传统项目管理面临信息孤岛、协同困难、进度不可控、成本超支等痛点。本方案旨在构建一个覆盖工程项目全生命周期、全参与方、全业务流程的一体化智能管理平台。1.2解决方案愿景打造数据驱动、智能协同、风险预警、自动执行的工程大脑,实现:管理流程自动化率≥80%项目协同效率提升40%成本偏差率降低至±3%以内安全事故发生率降低60%1.3目标用户矩阵┌───────────────┬
  • 行业资讯
  • 来源:源码码网
  • 编辑:源码码网
  • 时间:2026-01-09 11:26
  • 阅读:169
  • 车辆管理系统需求文档与技术架构PC端+小程序
  • 车辆管理系统需求文档与技术架构PC端+小程序
  • 第一部分:需求文档1.项目概述1.1项目背景为企事业单位、车队运营商、租赁公司等提供一套完整的车辆全生命周期管理解决方案,实现车辆管理数字化、智能化。1.2项目目标建立车辆从购置到报废的全流程管理体系实现用车申请、调度、监控、结算的闭环管理通过数据分析优化车辆使用效率降低车辆运维成本20%以上1.3用户角色矩阵┌──────────────┬─────────────────────────────┬──────────────
  • 行业资讯
  • 来源:源码码网
  • 编辑:源码码网
  • 时间:2026-01-09 11:11
  • 阅读:157
  • 智慧农业/渔业物联网系统需求文档
  • 智慧农业/渔业物联网系统需求文档
  • 智慧农业/渔业物联网系统需求文档文档版本: V1.0项目目标: 构建一个集环境智能监测、设备自动化控制、生长模型分析、溯源管理与远程指挥于一体的综合物联网管理平台,实现降本增效、提质增产、风险预警与品牌增值。1.系统总体概述1.1核心价值: 数据驱动决策,解放人力,实现农业/渔业生产的精准化、自动化与智能化。1.2用户角色:生产员/养殖员: 现场巡视、接收告警、执行设备手动控制、查看实时环境
  • 行业资讯
  • 来源:源码码网
  • 编辑:源码码网
  • 时间:2026-01-09 11:04
  • 阅读:68
  • 程序员AI编程工具推荐
  • 程序员AI编程工具推荐
  • AI编程工具是当前开发者的“副驾驶”,能够极大提升开发效率。以下我将从通用型、代码专用型、垂直领域型以及开源/自部署型几个维度为您分类推荐,并附上它们的核心特点和适用场景,帮助您选择。一、通用型AI对话助手(编程是核心能力之一)这类工具本质是“更懂代码的ChatGPT”,适合处理广泛的编程问题、解释代码、生成文档等。ChatGPT(GPT-4/4o)简介:行业标杆,尤其在GPT-4版本下,代码理解和生成能力极强。优点:上下文能力强,
  • 源码教程
  • 来源:源码码网
  • 编辑:源码码网
  • 时间:2026-01-09 10:56
  • 阅读:96
联系客服
源码代售 源码咨询 技术开发 联系客服
029-84538663
手机版

扫一扫进手机版
返回顶部