源码教程 2025年06月7日
0 收藏 0 点赞 1,005 浏览 3074 个字
摘要 :

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

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 所示。

Android实现电话拨打功能

图 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 所示。

Android实现电话拨打功能

图 2  拨打电话

微信扫一扫

支付宝扫一扫

版权: 转载请注明出处:https://www.zuozi.net/976.html

管理员

相关推荐
2025-07-05

对于一些使用WordPress进行外贸建站的商家来说,大部分人会通过在WordPress中添加JS代码和CSS样式表…

726
2025-07-05

当商家遇到WordPress独立站改版或者域名到期等问题时,不免会涉及到WordPress域名的更改,那么这一…

744
2025-07-05

用户在使用WooCommerce进行跨境电商独立站搭建工作时,有可能会借助WooCommerce短代码实现更加醒目…

332
2025-07-05

随着外贸建站行业的不断深入发展,WordPress的多语言功能也显得越来越重要,一个具有多语言的独立站…

1,077
2025-07-05

WooCommerce作为WordPress外贸建站生态的一部分,WooCommerce运费设置是商家在建站过程中不可忽视的…

868
2025-07-05

在外贸建站的过程中,有些商家会选择使用WordPress幻灯片为网站页面增加一定的动感和可观性,进而提…

751
发表评论
暂无评论

还没有评论呢,快来抢沙发~

助力内容变现

将您的收入提升到一个新的水平

点击联系客服

在线时间:08:00-23:00

客服QQ

122325244

客服电话

400-888-8888

客服邮箱

122325244@qq.com

扫描二维码

关注微信客服号