Implementation of phone dialing in Android studio case

Time:2022-1-2

1、 Code configuration

1. Create project

Flow chart

2. Add code

Change layout

Complete layout 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 = "phone dialing"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</LinearLayout>

Insert picture

activity_ main_ XML file layout

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 = "phone dialing"
        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 = "enter phone number"
        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

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

Run code

When dialing, there is an error. You need to set the corresponding permission

3. Permission request

Androidmainfest. XML file


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

For Android 6.0 and above, you need to give permission manually.

Application permission request
Version number judgment method


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

Permission application method


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

Call in onCreate


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

When requesting permission to add code, ensure that the program is running, otherwise the code will report an error.

2、 Effect demonstration

Enter the number casually, and the prompt is blank.

The above is the details of the implementation of phone dialing in the Android studio case. For more information about phone dialing in Android studio, please pay attention to other relevant articles of developeppaer!