ภาพรวม
ในบางสถานการณ์ ต้องการเปิดแอป YouTube จากแอปของเราเพื่อเล่นวิดีโอเฉพาะ ตัวอย่างนี้สาธิตวิธีใช้ Intent เปิด YouTube app
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
สร้างโปรเจกต์ Android ใหม่ใน Android Studio
ขั้นตอนที่ 2: เพิ่ม Layout XML
ไฟล์ res/layout/activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/parent"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/openYoutube"
android:text="Open youtube application"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
ขั้นตอนที่ 3: เพิ่มโค้ด Java
ไฟล์ src/MainActivity.java:
package com.example.andy.myapplication;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView openYoutube = findViewById(R.id.openYoutube);
openYoutube.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// สร้าง Intent เปิด YouTube ด้วย URL วิดีโอ
Intent webIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://www.youtube.com/watch?v=5X7WWVTrBvM"));
try {
startActivity(webIntent);
} catch (ActivityNotFoundException ex) {
// จัดการกรณีไม่มีแอป YouTube ติดตั้ง
// ex.printStackTrace();
}
}
});
}
}
การทำงานของ Intent
Intent webIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://www.youtube.com/watch?v=5X7WWVTrBvM"));
startActivity(webIntent);
ACTION_VIEW- แสดงข้อมูลให้ผู้ใช้ (ดูวิดีโอ)Uri.parse()- แปลง String URL เป็น Uri object- Android จะเลือกแอปที่รองรับ URL นี้ (YouTube app จะจับ YouTube URLs)
รูปแบบ URL YouTube ที่รองรับ
| รูปแบบ URL | ตัวอย่าง |
|---|---|
| Watch URL | https://www.youtube.com/watch?v=VIDEO_ID |
| Short URL | https://youtu.be/VIDEO_ID |
| Embed URL | https://www.youtube.com/embed/VIDEO_ID |
| Channel | https://www.youtube.com/channel/CHANNEL_ID |
| Playlist | https://www.youtube.com/playlist?list=PLAYLIST_ID |
🔧 แนวทางปฏิบัติที่ดีกว่า (Best Practices)
1. ตรวจสอบว่ามีแอปรับลองไว้หรือไม่
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(webIntent, 0);
boolean hasYouTube = false;
for (ResolveInfo info : activities) {
if (info.activityInfo.packageName.startsWith("com.google.android.youtube")) {
hasYouTube = true;
break;
}
}
if (!hasYouTube) {
// เปิดในเบราว์เซอร์แทน หรือแสดงข้อความ
webIntent.setData(Uri.parse("https://www.youtube.com/watch?v=5X7WWVTrBvM"));
}
2. ใช้ Intent Chooser ให้ผู้ใช้เลือก
Intent chooser = Intent.createChooser(webIntent, "Open with..."); startActivity(chooser);
3. Deep Link ไป YouTube App โดยตรง (Package specific)
Intent appIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("vnd.youtube:5X7WWVTrBvM")); // YouTube URI scheme
appIntent.setPackage("com.google.android.youtube");
try {
startActivity(appIntent);
} catch (ActivityNotFoundException e) {
// Fallback ไปเว็บ
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("https://www.youtube.com/watch?v=5X7WWVTrBvM")));
}
วิธีทดสอบ
- รันแอปบนอุปกรณ์ที่มี YouTube app ติดตั้ง
- คลิก "Open youtube application"
- จะเปิด YouTube app เล่นวิดีโอที่ระบุ
- ถ้าไม่มี YouTube app จะเปิดในเบราว์เซอร์ (หรือไม่เกิดอะไรขึ้นถ้าไม่มี catch)