Tính năng gọi điện thoại trong Android bằng Java

Jan 19 2023
Triển khai tính năng gọi điện thoại trong ứng dụng bằng Java trong Android. Dịch vụ gọi điện là một trong những tính năng cốt lõi trong Android và được sử dụng rất phổ biến trong Ứng dụng Android.

Triển khai tính năng gọi điện thoại trong ứng dụng bằng Java trong Android.

Dịch vụ gọi điện là một trong những tính năng cốt lõi trong Android và được sử dụng rất phổ biến trong Ứng dụng Android. Tính năng này cho phép người dùng thực hiện cuộc gọi từ bên trong ứng dụng Android. Có thể có hai cách để gọi cho ai đó thông qua ứng dụng Android. Một trong đó số được gọi được xác định trước và không yêu cầu người dùng nhập . Ví dụ: nêu thắc mắc hoặc hỗ trợ chăm sóc khách hàng. Cách tiếp cận thứ hai là gọi vào bất kỳ số hợp lệ nào bạn chọn. Theo cách tiếp cận này, người dùng phải nhập một số cần quay số. Trong Java, cả hai đều có thể được triển khai bằng chính gói dựng sẵn.

Chúng tôi cần cấp quyền để cho phép ứng dụng sử dụng tính năng gọi android sẵn có. Vì vậy, trước tiên, chúng tôi sẽ yêu cầu quyền CALL_PHONE từ người dùng bằng cách thêm mã quyền sau vào tệp AndroidManifest.xml .

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

Trong phần này, chúng tôi đã thêm hai nút và gán id cho cả hai nút. Một để gọi số được xác định trước và một để gọi thông qua đầu vào của người dùng.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="24dp"
        android:textStyle="bold"
        android:text="@string/call_feature" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="16dp"
        android:layout_marginTop="20dp"
        android:text="@string/feature" />

    <Button
        android:id="@+id/btnCall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="10dp"
        android:text="@string/contact_us"/>

    <EditText
        android:id="@+id/edtPhoneNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="16dp"
        android:layout_marginTop="20dp"
        android:hint="@string/dynamicCall"
        android:text="" />

    <Button
        android:id="@+id/btnCallDynamic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="10dp"
        android:text="@string/call"/>
</LinearLayout>

Chúng tôi sẽ sử dụng một Ý định Android để gọi. Một ý định android bao gồm mã bạn cần để thực hiện một hành động và mô tả dữ liệu trong một đối tượng Ý định mà bạn yêu cầu từ hệ thống. Một ý định cho phép bạn bắt đầu một hoạt động trong một ứng dụng khác bằng cách mô tả một hành động đơn giản mà bạn muốn thực hiện trong một đối tượng Ý định.

Ở đây, chúng ta đang tạo một Ý định và mô tả hành động ACTION_CALL trong Đối tượng Ý định.

Intent callIntent = new Intent(Intent.ACTION_CALL);

callIntent.setData(Uri.parse("tel: 9999999999"));

ActivityCompat.requestPermissions(MainActivity.this, 
            new String[]{Manifest.permission.CALL_PHONE}, REQUEST_PHONE_CALL);

startActivity(callIntent);

package com.example.androidphonecalls;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

// Initialized edittext and buttons
    Button call, dynamicCall;
    EditText phoneNumber;
    private static final int REQUEST_PHONE_CALL = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

// Getting instance of button and edit text
        call = findViewById(R.id.btnCall);
        phoneNumber = findViewById(R.id.edtPhoneNumber);
        dynamicCall = findViewById(R.id.btnCallDynamic);

// Here we are adding onClick listener on the button to call predefined number
        call.setOnClickListener(view -> {
            Intent callIntent = new Intent(Intent.ACTION_CALL);
// Here we have defined the number to be called
            callIntent.setData(Uri.parse("tel: 9999999999"));
// Checking for the permission
            ActivityCompat.requestPermissions(MainActivity.this, 
            new String[]{Manifest.permission.CALL_PHONE}, REQUEST_PHONE_CALL);
// Starting the call Intent
            startActivity(callIntent);
        });

// Here we are adding onClick listener on the button to call via user input
        dynamicCall.setOnClickListener(view -> {
// Take value from user input
            String retrieveNumber = phoneNumber.getText().toString();
            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:" + retrieveNumber));
// Checking  for permissions
            ActivityCompat.requestPermissions(MainActivity.this, 
            new String[]{Manifest.permission.CALL_PHONE}, REQUEST_PHONE_CALL);
// Starting the call Intent
            startActivity(callIntent);
        });
    }
}

Cảm ơn !