अन्य ऐप लॉन्च करना
Android में, हम पैकेज नाम का उपयोग करके अन्य एप्लिकेशन लॉन्च कर सकते हैं। यह उदाहरण दर्शाता है कि एक ऐप से दूसरे ऐप (जैसे YouTube) को कैसे लॉन्च किया जाए।
उदाहरण: YouTube ऐप लॉन्च करना
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project चुनें।
चरण 2: लेआउट फ़ाइल (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:background="#33FFFF00"
android:orientation="vertical">
<TextView
android:id="@+id/text"
android:textSize="18sp"
android:text="Click to open youtube"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
चरण 3: MainActivity.java में कोड
package com.example.andy.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
int view = R.layout.activity_main;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(view);
final LinearLayout parent = findViewById(R.id.parent);
textView = findViewById(R.id.text);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.google.android.youtube");
if (launchIntent != null) {
startActivity(launchIntent);
} else {
Toast.makeText(MainActivity.this, "There is no package available in android", Toast.LENGTH_LONG).show();
}
}
});
}
}
कोड व्याख्या
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.google.android.youtube");
if (launchIntent != null) {
startActivity(launchIntent);
} else {
Toast.makeText(MainActivity.this, "There is no package available in android", Toast.LENGTH_LONG).show();
}
उपरोक्त कोड में, हमने YouTube का पैकेज नाम उपयोग किया है। यदि पैकेज मौजूद है तो ऐप लॉन्च होता है, अन्यथा टोस्ट संदेश दिखाता है।
ऐप चलाएं और परीक्षण करें
ऐप चलाएं। डिफ़ॉल्ट स्क्रीन दिखेगी। TextView पर क्लिक करने पर YouTube ऐप खुलेगा (यदि इंस्टॉल है)।

YouTube खुलने के बाद:

मुख्य बिंदु
- getLaunchIntentForPackage() पैकेज नाम से लॉन्च इंटेंट प्राप्त करता है।
- हमेशा जांचें कि इंटेंट null नहीं है (ऐप इंस्टॉल नहीं होने पर)।
- किसी भी ऐप को लॉन्च करने के लिए उसका पैकेज नाम जानना आवश्यक है।
- यह विधि केवल उन्हीं ऐप्स के लिए काम करती है जिनमें लॉन्च करने योग्य एक्टिविटी हो।