ภาพรวม
ตัวอย่างนี้แสดงวิธีเปิดแอปพลิเคชันอื่น (เช่น YouTube) จากแอปของคุณโดยใช้ PackageManager และ Intent
ขั้นตอนการสร้าง
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
เปิด Android Studio ไปที่ File → New Project และกรอกข้อมูลที่จำเป็น
ขั้นตอนที่ 2: Layout (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<Button
android:id="@+id/applist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="27dp"
android:text="Open YouTube" />
</LinearLayout>
ขั้นตอนที่ 3: โค้ดหลัก (src/MainActivity.java)
package com.example.myapplication;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnOpenApp = findViewById(R.id.applist);
btnOpenApp.setOnClickListener(v -> openYouTubeApp());
}
private void openYouTubeApp() {
// วิธีที่ 1: ใช้ Package Name โดยตรง (เร็วที่สุด)
Intent intent = getPackageManager().getLaunchIntentForPackage("com.google.android.youtube");
if (intent != null) {
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
showToast("YouTube app not found");
}
} else {
showToast("YouTube app not installed");
}
}
private void showToast(String message) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
}
วิธีอื่นๆ ในการเปิดแอปอื่น
1. ใช้ Implicit Intent (แนะนำสำหรับการกระทำทั่วไป)
// เปิดเว็บไซต์
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.youtube.com"));
startActivity(intent);
// เปิดแอปกล้อง
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivity(intent);
// แชร์ข้อความ
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "Hello World");
startActivity(Intent.createChooser(intent, "Share via"));
2. ตรวจสอบว่าแอปติดตั้งอยู่หรือไม่
public boolean isAppInstalled(String packageName) {
PackageManager pm = getPackageManager();
try {
pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
return true;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}
// ใช้งาน
if (isAppInstalled("com.google.android.youtube")) {
// เปิด YouTube
} else {
// นำไป Play Store
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("market://details?id=com.google.android.youtube"));
startActivity(intent);
}
3. ใช้ PackageManager queryIntentActivities (สำหรับแสดงรายการแอปที่รองรับ)
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
List<ResolveInfo> activities = getPackageManager().queryIntentActivities(intent, 0);
// แสดงใน RecyclerView ให้ผู้ใช้เลือก
for (ResolveInfo info : activities) {
String appName = info.loadLabel(getPackageManager()).toString();
String packageName = info.activityInfo.packageName;
// เพิ่มใน Adapter
}
4. Android 11+ (API 30) ต้องเพิ่ม <queries> ใน Manifest
<manifest ...>
<queries>
<package android:name="com.google.android.youtube" />
<!-- หรือ -->
<intent>
<action android:name="android.intent.action.SEND" />
<data android:mimeType="text/plain" />
</intent>
</queries>
<application ...>...</application>
</manifest>
สรุปแนวทางเลือก
| วิธี | เหมาะสำหรับ | ข้อควรระวัง |
|---|---|---|
| getLaunchIntentForPackage | เปิดแอปเฉพาะเจาะจง | ต้องรู้ Package Name ต้องมี <queries> API 30+ |
| Implicit Intent (ACTION_VIEW, SEND, etc.) | การกระทำทั่วไป | ผู้ใช้เลือกแอปเอง ให้ UX ดีกว่า |
| PackageManager query | แสดงรายการแอปที่รองรับ | ซับซ้อนกว่า ต้องทำ UI เอง |