Android 实现拨打电话功能

Android 实现拨打电话功能

Android 实现拨打电话功能;

首先会检测应用是否拥有拨打电话的权限,会动态申请;

从页面输入手机号码后,点击按钮就会调用系统的拨号功能;

也可以设置为默认的电话号码,作为客服联系方式。

MakeCallActivity.Java

java 复制代码
import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

public class MakeCallActivity extends Activity implements View.OnClickListener {
    Button btn_make_call;
    EditText phoneNumber;
    private static final int REQUEST_PHONE_CALL = 1;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_make_call);
        phoneNumber = (EditText) findViewById(R.id.phoneNumber);
        btn_make_call = findViewById(R.id.btn_make_call);
        btn_make_call.setOnClickListener(this);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        Log.e("permission", requestCode + "");
        switch (requestCode) {
            case 1:
                if (requestCode == REQUEST_PHONE_CALL) {
                    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                        call(phoneNumber.getText().toString()); // 使用已授权的电话号码
                    } else {
                        Toast.makeText(this, "电话权限被拒绝", Toast.LENGTH_SHORT).show();
                    }
                }
                break;
            default:
                Toast.makeText(this, "wait", Toast.LENGTH_LONG).show();
        }

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.btn_make_call) {
            if (ContextCompat.checkSelfPermission(MakeCallActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(MakeCallActivity.this, new String[]{Manifest.permission.CALL_PHONE}, 1);
                call(phoneNumber.getText().toString());
                Log.e("permission", "requestPermissions");
            } else {
                call(phoneNumber.getText().toString());
                Log.e("permission", "checkSelfPermission");
            }

        }

    }


    private void call(String phone) {
        try {
            Intent intent = new Intent(Intent.ACTION_CALL);
            intent.setData(Uri.parse("tel:" + phone));
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
                startActivity(intent);
            }
        } catch (SecurityException e) {
            e.printStackTrace();
        }
    }
}

activity_make_call.xml

xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="电话号码:"/>
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入电话号码"
            android:id="@+id/phoneNumber"
            />
    </LinearLayout>
    <Button
        android:id="@+id/btn_make_call"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/make_call" />
</LinearLayout>

AndroidManifest.xml

xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <uses-feature
        android:name="android.hardware.telephony"
        android:required="true" />
    <uses-permission android:name="android.permission.CALL_PHONE" />


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/make_call"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Learn">
        <activity
            android:name=".MakeCallActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
相关推荐
千里马学框架15 小时前
一起学 Android 14:ShellTransition 屏幕旋转过程深度剖析
android·智能手机·性能优化·framework·性能·屏幕旋转·rotation
美狐美颜SDK开放平台15 小时前
开发直播APP时如何接入视频美颜SDK?开发流程与注意事项
android·人工智能·计算机视觉·音视频·直播美颜sdk
AFinalStone16 小时前
Android7 SystemUI源码解析(七)Keyguard锁屏模块深度解析
android·systemui
致远ccc16 小时前
Google Play 上架前如何测试 App?多国家 Android 环境测试
android·app测试·googleplay·多国家应用测试
ttyyttemo18 小时前
Kotlin 协程中的 Job 结构化并发与取消
android
sun00770018 小时前
tbox 4g/5g切换,导致wan ip 改变,导致车机旧网络不可用。需要重启车机才行
android
其实防守也摸鱼19 小时前
内网穿透与反向代理:原理、工具与实战指南
android·大数据·运维·安全·网络安全·自动化·渗透
AFinalStone20 小时前
Android7 SystemUI 源码解析(四)NavigationBar 导航栏与 SystemBars
android·systemui
JMchen20 小时前
属性动画原理与高级动画实现
android·kotlin·canvas
AFinalStone20 小时前
Android7 SystemUI 源码解析(二)启动流程深度解析
android·systemui