ภาพรวม
ตัวอย่างนี้สาธิตวิธีการโหลดลิงก์ YouTube ใน WebView ซึ่งจะกระตุ้นให้ Android เปิด แอป YouTube ต้นฉบับ (Native App) แทนการเล่นใน WebView โดยอัตโนมัติ (ผ่าน Intent Filter)
ขั้นตอนที่ 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:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<WebView
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
ในโค้ดข้างต้น เราใช้ WebView เต็มหน้าจอเพื่อโหลดลิงก์ YouTube
ขั้นตอนที่ 3: เขียนโค้ดหลัก (src/MainActivity.java)
package com.example.myapplication;
import android.app.ProgressDialog;
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.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class MainActivity extends AppCompatActivity {
@RequiresApi(api = Build.VERSION_CODES.P)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading Data...");
progressDialog.setCancelable(false);
WebView webView = findViewById(R.id.web_view);
webView.requestFocus();
webView.getSettings().setLightTouchEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setGeolocationEnabled(true);
webView.setSoundEffectsEnabled(true);
// โหลดลิงก์ YouTube (จะกระตุ้นเปิดแอป YouTube ถ้าติดตั้งไว้)
webView.loadUrl("https://www.youtube.com/watch?v=atnyX5yjLj0");
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
if (newProgress < 100) {
progressDialog.show();
} else {
progressDialog.dismiss();
}
}
});
}
}
คำอธิบายสำคัญ:
- ลิงก์ YouTube (
youtube.com/watch?v=...หรือyoutu.be/...) มี Intent Filter ในแอป YouTube - เมื่อ WebView โหลดลิงก์นี้ Android จะแสดง App Chooser Dialog ให้ผู้ใช้เลือกเปิดด้วย "YouTube" หรือ "Chrome" หรือ "WebView"
- หากผู้ใช้เลือก "YouTube" และติ๊ก "Always" จะเปิดแอป YouTube ต้นฉบับโดยอัตโนมัติครั้งต่อไป
- การทำงานนี้ขึ้นอยู่กับ Android Intent System ไม่ใช่โค้ด WebView โดยตรง
หากต้องการบังคับเปิดในแอป YouTube เสมอ (ไม่แสดง Dialog) ใช้
WebViewClient.shouldOverrideUrlLoading():
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
String url = request.getUrl().toString();
if (url.contains("youtube.com") || url.contains("youtu.be")) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.setPackage("com.google.android.youtube"); // บังคับแอป YouTube
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
return true;
}
}
return false;
}
});
โค้ดต้นฉบับมีเงื่อนไข
if (progress = = 100) ซึ่งเป็น Syntax Error (ต้องเป็น == ไม่มีช่องว่าง) และ URL มีช่องว่าง v = atnyX5yjLj0 → v=atnyX5yjLj0 - ได้แก้ไขแล้ว
ขั้นตอนที่ 4: กำหนดสิทธิ์ใน AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<uses-permission android:name="android.permission.INTERNET"/>
<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 จริงกับคอมพิวเตอร์ (ต้องติดตั้งแอป YouTube ไว้) ใน Android Studio เปิดไฟล์ Activity แล้วคลิกไอคอน Run จากแถบเครื่องมือ เลือกอุปกรณ์มือถือของคุณ แล้วตรวจสอบหน้าจอ - จะแสดง Dialog ให้เลือกเปิดด้วย YouTube หรือ Browser
