Android studio电话拨号 Android studio案例之实现电话拨号

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Android studio电话拨号 Android studio案例之实现电话拨号

天会晴就会暗   2021-04-12 我要评论
想了解Android studio案例之实现电话拨号的相关内容吗,天会晴就会暗在本文为您仔细讲解Android studio电话拨号的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,studio,电话拨号,Android,电话拨号,下面大家一起来学习吧。

 一、代码配置

1、创建项目

流程看图

2、增添代码

更改布局

布局完整代码

<?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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="电话拨号"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</LinearLayout>

插入图片

activity_main_xml文件布局

完整代码

<?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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="电话拨号"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="输入电话号码"
        android:inputType="number"
        android:id="@+id/phoneNum"/>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/call"
            android:layout_centerInParent="true" 
            android:id="@+id/call_btn"/>
    </RelativeLayout>
</LinearLayout>

效果展示

mainactivity.java文件

 EditText phoneNum;
    ImageButton call_btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        phoneNum=(EditText) findViewById(R.id.phoneNum);
        call_btn=(ImageButton) findViewById(R.id.call_btn);
       call_btn.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Intent intent=new Intent();
               intent.setAction(Intent.ACTION_CALL);
               intent.setData(Uri.parse("tel:"+phoneNum.getText()));
               startActivity(intent);

图片

运行代码

拨号尝试,出现报错,需要设置对应权限

3、权限请求

Androidmainfest.xml文件中

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

Android 6.0以上需要自己手动赋予权限。

应用程序权限请求
版本号判断方法

protected boolean shouldAskPermissions(){
    return (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1);
}

权限申请方法

protected void askPermissions() {
    String[] permissions = {
            "android.permission.CALL_PHONE"
    };
    int requestCode = 200;
    requestPermissions(permissions, requestCode);
}

在onCreate中调用

if(shouldAskPermissions()){
    askPermissions();
}

请求权限加入代码时要保证程序处于运行状态,不然代码加进去会报错。

二、效果演示

随便输入得数字号码,提示为空号。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们