广告
返回顶部
首页 > 资讯 > 移动开发 >Android实现拨打电话功能
  • 462
分享到

Android实现拨打电话功能

androidjava开发语言拨打电话功能 2023-09-13 20:09:37 462人浏览 独家记忆
摘要

Android实现拨打电话功能 先上UI效果图点击联系博主拒绝权限同意权限直接拨号 跳转到拨号界面跳转到拨号界面 同时附带号码 代码实现AndroidManifest.xml中添加权

Android实现拨打电话功能

先上UI效果图

点击联系博主

在这里插入图片描述

拒绝权限

在这里插入图片描述

同意权限

直接拨号

在这里插入图片描述

跳转到拨号界面

在这里插入图片描述

跳转到拨号界面 同时附带号码

在这里插入图片描述

代码实现

AndroidManifest.xml中添加权限

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

点击联系博主

contactBlogger.setOnClickListener(v -> {            callPhone();//            callUI();//            callPhoneUI();        });

判断是否有拨号权限

// 判断是否有拨号权限    private boolean ifHaveCallPhonePermission() {        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {            // 动态申请权限            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, CALL_PHONE_REQUEST_CODE);            return false;        }        return true;    }

申请权限回调

// 申请权限回调    @Override    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {        super.onRequestPermissionsResult(requestCode, permissions, grantResults);        if (requestCode == CALL_PHONE_REQUEST_CODE) {            if (permissions.length != 0 && grantResults[0] != PackageManager.PERMISSION_GRANTED) {                Toast.makeText(this, "请添加拨号权限后重试", Toast.LENGTH_SHORT).show();            } else {                callPhone();//                callUI();//                callPhoneUI();            }        }    }

直接拨号

// 直接拨号    private void callPhone() {        if (ifHaveCallPhonePermission()) {            startActivity(new Intent(Intent.ACTION_CALL, Uri.parse(phone)));        }    }

跳转到拨号界面

// 跳转到拨号界面    private void callUI() {        if (ifHaveCallPhonePermission()) {            startActivity(new Intent(Intent.ACTION_CALL_BUTTON));        }    }

跳转到拨号界面 同时附带号码

// 跳转到拨号界面 同时附带号码    private void callPhoneUI() {        if (ifHaveCallPhonePermission()) {            startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(phone)));        }    }

完整代码

MainActivity.java

package top.gaojc.callphonedemo;import androidx.annotation.NonNull;import androidx.appcompat.app.AppCompatActivity;import androidx.core.app.ActivityCompat;import android.Manifest;import android.content.Intent;import android.content.pm.PackageManager;import android.net.Uri;import android.os.Bundle;import android.widget.Button;import android.widget.Toast;public class MainActivity extends AppCompatActivity {    private Button contactBlogger;    private final String phone = "tel:13103679960";    private final int CALL_PHONE_REQUEST_CODE = 10001;//拨号请求码    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        contactBlogger = findViewById(R.id.contact_blogger);        contactBlogger.setOnClickListener(v -> {            callPhone();//            callUI();//            callPhoneUI();        });    }    // 判断是否有拨号权限    private boolean ifHaveCallPhonePermission() {        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {            // 动态申请权限            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, CALL_PHONE_REQUEST_CODE);            return false;        }        return true;    }    // 申请权限回调    @Override    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {        super.onRequestPermissionsResult(requestCode, permissions, grantResults);        if (requestCode == CALL_PHONE_REQUEST_CODE) {            if (permissions.length != 0 && grantResults[0] != PackageManager.PERMISSION_GRANTED) {                Toast.makeText(this, "请添加拨号权限后重试", Toast.LENGTH_SHORT).show();            } else {                callPhone();//                callUI();//                callPhoneUI();            }        }    }    // 直接拨号    private void callPhone() {        if (ifHaveCallPhonePermission()) {            startActivity(new Intent(Intent.ACTION_CALL, Uri.parse(phone)));        }    }    // 跳转到拨号界面    private void callUI() {        if (ifHaveCallPhonePermission()) {            startActivity(new Intent(Intent.ACTION_CALL_BUTTON));        }    }    // 跳转到拨号界面 同时附带号码    private void callPhoneUI() {        if (ifHaveCallPhonePermission()) {            startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(phone)));        }    }}

activity_main.xml

<FrameLayout xmlns:android="Http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity">    <Button        android:id="@+id/contact_blogger"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:text="@string/contact_blogger" />FrameLayout>

demo下载

demo下载

来源地址:https://blog.csdn.net/Mr_Gaojinchao/article/details/131295883

--结束END--

本文标题: Android实现拨打电话功能

本文链接: https://www.lsjlt.com/news/406346.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作