Android 实现打电话(附源码)

517 阅读2分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第14天,点击查看活动详情

前言

  本教程是 Android 实现拨打号码的 Demo 用来练习的案例。

  Android一词的本义指“机器人”,同时也是Google于2007年11月5日宣布的基于Linux平台的开源手机操作系统的名称,该平台由操作系统、中间件、用户界面应用软件组成。

image-20221012211953969.png

1、需求分析

** 点击按钮拨打指定**

  打电话是手机的一个最基本的功能,现在android智能手机非常流行,里面有多种多样的精彩的手机功能,其实android手机如何实现打电话这个基本功能也很简单,本案例使用AndroidStudio+逍遥模拟器开发环境,使用java语言开发。

2、实现思路

  1. 在布局文件中使用线性布局
  2. 添加Button按钮控件,并其设置ID
  3. 在java代码中给Button设置点击事件
  4. 编写核心代码,拨打电话等

3、目录结构

image-20221012211637852.png

4、具体实现

1、具体实现代码

具体实现代码是一个点击事件和拨打电话事件,其先调用拨号界面再调用拨号功能。

详细代码如下 :

package com.example.callphone;
​
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
​
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.view.View;
import android.widget.Button;
import android.widget.Toast;
​
public class MainActivity extends AppCompatActivity
{
​
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button=findViewById(R.id.btnCall);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE)!=
                        PackageManager.PERMISSION_GRANTED)
                {
                    ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.CALL_PHONE},1);
                }
                else
                {
                    call();
                }
            }
        });
    }
    public void call()
    {
        try
        {
            Intent intent = new Intent(Intent.ACTION_CALL);
            intent.setData(Uri.parse("tel:10086"));
            startActivity(intent);
        }
        catch (SecurityException e)
        {
            e.printStackTrace();
        }
    }
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,@NonNull int[] grantResults)
    {
        switch(requestCode)
        {
            case 1:
                if(grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED)
                {
                    call();
                }
                else
                {
                    Toast.makeText(this,"你拒绝了申请!",Toast.LENGTH_LONG).show();
                }
        }
    }
}
​

2、布局代码

布局代码主要是线性布局和一个Button按钮。

详细代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/btnCall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点击拨打电话"
        android:layout_marginTop="100dp"
        android:layout_marginLeft="200dp"
        />
​
</LinearLayout>

5、效果演示

1、运行截图

图片2.png

2、拨打成功

图片1.png

总结

  以上就是 Android 拨打号码的全部内容,其实也很简单,主要是一个点击事件,一个call拨号方法和拒绝方法,实现起来也不难,希望大家在Android的学习之路更上一层楼。