Android studio case-realizing phone dialing

table of Contents

One, code configuration

1. Create a project

Flow chart
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

2. Add code

Change the layout
Insert picture description here
Layout the complete code

<?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>

Insert picture
Insert picture description here

activity_main_xml file layout
Insert picture description here
complete code

<?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>

Effect display
Insert picture description here
mainactivity.java file

 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);

Picture
Insert picture description here
Run code
Insert picture description here
Try to dial, an error occurs, you need to set the corresponding permissions
Insert picture description here

3. Permission request

Androidmainfest.xml file

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

Insert picture description here

Android 6.0 and above need to manually grant permissions by yourself.

Application permission request
version number judgment method

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

How to apply for permission

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

Call in onCreate

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

Insert picture description here

When requesting permission to add code, make sure that the program is running, otherwise an error will be reported when the code is added.

Second, the effect demonstration

Insert picture description here
Insert picture description here

Insert picture description here

Enter the number randomly, and the prompt is a blank number.

Guess you like

Origin blog.csdn.net/QWERTYzxw/article/details/115094179