Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีเปิด Dialer พร้อมแสดงหมายเลขโทรศัพท์ใน Android

ภาพรวม

ตัวอย่างนี้สาธิตวิธีเปิดแอปพลิเคชันโทรศัพท์ (Dialer) พร้อมใส่หมายเลขโทรศัพท์ล่วงหน้าในแอป Android โดยใช้ Intent.ACTION_DIAL ซึ่งเป็นวิธีที่ปลอดภัยและไม่ต้องการสิทธิ์ CALL_PHONE โดยตรง

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

เปิด Android Studio → File → New Project → เลือก Empty Activity → กรอกรายละเอียดโปรเจกต์ให้ครบถ้วน

ขั้นตอนที่ 2: ออกแบบ Layout (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="เปิด Dialer ด้วยหมายเลข" />

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

</RelativeLayout>

ขั้นตอนที่ 3: เขียนโค้ด MainActivity (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);
    }
}

ขั้นตอนที่ 4: กำหนดค่า AndroidManifest.xml

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

    <!-- ไม่จำเป็นต้องมี CALL_PHONE สำหรับ ACTION_DIAL -->
    <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>

การทดสอบ

เชื่อมต่ออุปกรณ์ Android จริงกับคอมพิวเตอร์ → กดปุ่ม Run ใน Android Studio → เลือกอุปกรณ์ → แอปจะเปิด Dialer พร้อมหมายเลข 0123456789

หมายเหตุสำคัญ

  • ใช้ ACTION_DIAL แทน ACTION_CALL เพื่อไม่ต้องขอสิทธิ์ CALL_PHONE
  • ผู้ใช้ต้องกดปุ่มโทรเองด้วยตนเอง (เพื่อความปลอดภัย)
  • รองรับรูปแบบหมายเลขต่างๆ เช่น tel:+66812345678