परिचय
एक Intent Filter, IntentFilter क्लास का एक उदाहरण है। Intent Filters implicit intents का उपयोग करते समय सहायक होते हैं, इसे जावा कोड में हैंडल नहीं किया जाता है, हमें इसे AndroidManifest.xml में सेट करना होता है। Android को पता होना चाहिए कि वह किस तरह का Intent लॉन्च कर रहा है, इसलिए Intent Filters Android को Intent और Actions के बारे में जानकारी देते हैं।
Intent लॉन्च करने से पहले, Android एक्शन टेस्ट, कैटेगरी टेस्ट और डेटा टेस्ट करेगा। यह उदाहरण दर्शाता है कि Android में Intent Filters का उपयोग कैसे करें।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें।
चरण 2: 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">
<Button
android:id="@+id/buton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="intent filter button" />
</LinearLayout>
उपरोक्त में, हमने एक बटन दिया है जब आप बटन पर क्लिक करते हैं तो यह एक्शन के साथ Intent दिखाएगा।
चरण 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.Button;
import android.widget.RadioButton;
public class MainActivity extends AppCompatActivity {
RadioButton radioButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = findViewById(R.id.buton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"contact@tutorialspoint.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, "Welcome to tutorialspoint.com");
startActivity(Intent.createChooser(intent, "Choose default Mail App"));
}
});
}
}
उपरोक्त में जब उपयोगकर्ता बटन पर क्लिक करता है तो यह ACTION_SEND का उपयोग करके Intent को कॉल करेगा और टाइप को message/rfc882 के रूप में सेट करेगा। अब हमने ईमेल आईडी और विषय संदेश पास किया है।
चरण 4: Manifest.xml में Intent Filter जोड़ें
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.andy.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" />
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="message/rfc822" />
</intent-filter>
</activity>
</application>
</manifest>
उपरोक्त में हमने एक्शन, कैटेगरी और डेटा घोषित किया है।
ऐप चलाएं
मान लें कि आपने अपना वास्तविक Android मोबाइल डिवाइस कंप्यूटर से कनेक्ट किया है। Android Studio से ऐप चलाने के लिए, अपनी प्रोजेक्ट की एक्टिविटी फ़ाइलों में से एक खोलें और टूलबार से Run आइकन पर क्लिक करें। अपने मोबाइल डिवाइस को विकल्प के रूप में चुनें और फिर अपना मोबाइल डिवाइस जांचें जो डिफ़ॉल्ट स्क्रीन प्रदर्शित करेगा:

उपरोक्त बटन पर क्लिक करें यह Intent Chooser को कॉल करेगा ताकि Intent से डेटा भेजने के लिए एप्लिकेशन चुन सकें जैसा कि नीचे दिखाया गया है:

हमने Gmail एप्लिकेशन को चुना जैसा कि नीचे दिखाया गया है:

उपरोक्त परिणाम में, यह Intent से डेटा लेगा और Gmail एप्लिकेशन में जोड़ देगा।