परिचय
यह उदाहरण दर्शाता है कि Kotlin का उपयोग करके एंड्रॉइड ऐप से फेसबुक पेज कैसे खोला जाए - पहले फेसबुक ऐप में खोलने का प्रयास करता है, असफल होने पर ब्राउज़र में खोलता है।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio खोलें, File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें।
चरण 2: लेआउट 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/rlMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp"
android:onClick="fbClick"
android:text="फेसबुक खोलें" />
</LinearLayout>
चरण 3: Kotlin कोड लागू करें
src/MainActivity.kt में निम्नलिखित कोड जोड़ें:
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun fbClick(view: View) {
startActivity(getOpenFacebookIntent())
}
private fun getOpenFacebookIntent(): Intent? {
return try {
// फेसबुक ऐप इंस्टॉल है या नहीं चेक करें
packageManager.getPackageInfo("com.facebook.katana", 0)
// फेसबुक ऐप में पेज खोलें (fb:// URI स्कीम)
Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/426253597411506"))
} catch (e: Exception) {
// फेसबुक ऐप नहीं है तो ब्राउज़र में खोलें
Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/appetizerandroid"))
}
}
}
चरण 4: मैनिफेस्ट कॉन्फ़िगर करें
androidManifest.xml में निम्नलिखित कोड जोड़ें:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.q11">
<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 Studio में प्रोजेक्ट की एक्टिविटी फाइल खोलें और टूलबार से Run आइकन पर क्लिक करें। अपना मोबाइल डिवाइस चुनें और ऐप का डिफॉल्ट स्क्रीन देखें।
महत्वपूर्ण नोट्स
fb://page/<page_id>- फेसबुक ऐप के लिए URI स्कीम (पेज ID डालें)fb://profile/<user_id>- प्रोफाइल खोलने के लिए- फेसबुक ऐप अपडेट के साथ URI स्कीम बदल सकती है
- हमेशा फॉलबैक के रूप में वेब URL (https://facebook.com/...) प्रदान करें
- पैकेज नाम
com.facebook.katanaमुख्य फेसबुक ऐप के लिए है