Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Cách mở Dialer với số điện thoại hiển thị sẵn trên Android

Giới thiệu

Bài viết này hướng dẫn cách mở ứng dụng gọi điện (Dialer) với một số điện thoại được điền sẵn trên Android bằng cách sử dụng Intent ACTION_DIAL.

Các bước thực hiện

Bước 1: Tạo dự án mới

Mở Android Studio, chọn File → New Project và điền các thông tin cần thiết để tạo dự án mới.

Bước 2: Thiết kế giao diện (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="8dp"
    tools:context=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginTop="40dp"
        android:onClick="ShowDialer"
        android:text="Hiển thị Dialer" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:autoLink="phone"
        android:linksClickable="true" />

</RelativeLayout>

Bước 3: Viết code logic (src/MainActivity.java)

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

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

    public void ShowDialer(View view) {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        intent.setData(Uri.parse("tel:0123456789"));
        startActivity(intent);
    }
}

Bước 4: Cấu hình quyền trong AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.com.sample">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

Chạy ứng dụng

Kết nối thiết bị Android thực với máy tính. Trong Android Studio, mở file activity và nhấn biểu tượng Run trên thanh công cụ. Chọn thiết bị di động của bạn và kiểm tra màn hình thiết bị.

Giải thích quan trọng

  • Intent.ACTION_DIAL: Mở dialer với số điện thoại đã điền sẵn, người dùng phải nhấn nút gọi mới thực hiện cuộc gọi.
  • Uri.parse("tel:..."): Định dạng số điện thoại theo chuẩn URI.
  • Quyền CALL_PHONE chỉ cần thiết nếu dùng ACTION_CALL (gọi trực tiếp không qua dialer).