ACTION_VIEW इंटेंट का उपयोग करके डिफ़ॉल्ट ब्राउज़र में विशिष्ट URL खोलने का सरल तरीका।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में नया प्रोजेक्ट बनाएं।
चरण 2: लेआउट में बटन
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:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:onClick="GetUrlFromIntent"
android:text="Get URL from Intent" />
</RelativeLayout>
चरण 3: इंटेंट लॉजिक
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 GetUrlFromIntent(View view) {
String url = "https://www.google.com";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
}
}
चरण 4: मैनिफेस्ट में इंटरनेट अनुमति
<uses-permission android:name="android.permission.INTERNET"/>
मुख्य बिंदु
Intent.ACTION_VIEW- डेटा देखने के लिए मानक कार्यUri.parse()- स्ट्रिंग को URI में बदलता है- सिस्टम उपयुक्त ऐप चुनने के लिए चॉइस डायलॉग दिखाता है (ब्राउज़र, Chrome, आदि)
intent.setPackage("com.android.chrome")से विशिष्ट ब्राउज़र बाध्य कर सकते हैं
उन्नत उपयोग
// कस्टम टैब (Chrome Custom Tabs) - बेहतर UX के लिए CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder().build(); customTabsIntent.launchUrl(this, Uri.parse(url));
Chrome Custom Tabs के लिए implementation 'androidx.browser:browser:1.5.0' जोड़ें।