एंड्रॉइड WebView से YouTube ऐप लॉन्च करना
यह उदाहरण दर्शाता है कि WebView में YouTube लिंक पर क्लिक करने पर YouTube ऐप कैसे खोलें, बजाय WebView में ही वीडियो चलाने के।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में नया प्रोजेक्ट बनाएं।
चरण 2: लेआउट फ़ाइल
<?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>
चरण 3: MainActivity.java
package com.example.myapplication;
import android.app.ProgressDialog;
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.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
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("लोड हो रहा है...");
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 लिंक - WebViewClient से इंटरसेप्ट होगा
webView.loadUrl("https://www.youtube.com/watch?v=atnyX5yjLj0");
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// YouTube लिंक्स को ऐप में खोलें
if (url != null && (url.contains("youtube.com") || url.contains("youtu.be"))) {
openYouTubeApp(url);
return true; // WebView में लोड न करें
}
// अन्य लिंक्स WebView में ही लोड करें
view.loadUrl(url);
return true;
}
});
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int progress) {
if (progress < 100) {
progressDialog.show();
}
if (progress == 100) {
progressDialog.dismiss();
}
}
});
}
private void openYouTubeApp(String url) {
try {
// YouTube ऐप इंटेंट
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.setPackage("com.google.android.youtube");
// फॉलबैक: अगर YouTube ऐप नहीं है तो ब्राउज़र में खोलें
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
} else {
// YouTube ऐप नहीं है - ब्राउज़र में खोलें
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(browserIntent);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
चरण 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>
</intent-filter>
</activity>
</application>
</manifest>
मुख्य बिंदु
shouldOverrideUrlLoading()में YouTube URL डिटेक्ट करेंIntent.ACTION_VIEWके साथsetPackage("com.google.android.youtube")से YouTube ऐप टार्गेट करेंresolveActivity()से चेक करें कि ऐप इंस्टॉल है या नहीं- फॉलबैक: ऐप न होने पर ब्राउज़र में खोलें
अन्य ऐप्स के लिए पैटर्न
// Google Maps
if (url.contains("maps.google.com")) {
intent.setPackage("com.google.android.apps.maps");
}
// Twitter/X
if (url.contains("twitter.com") || url.contains("x.com")) {
intent.setPackage("com.twitter.android");
}
// Instagram
if (url.contains("instagram.com")) {
intent.setPackage("com.instagram.android");
}
ऐप चलाएं
YouTube वीडियो पेज लोड होगा - लिंक पर क्लिक करने पर YouTube ऐप खुलेगा (यदि इंस्टॉल है)।
