IntentFilter ক্লাসের একটি ইনস্ট্যান্স হলো ইন্টেন্ট ফিল্টার। ইম্প্লিসিট ইন্টেন্ট ব্যবহার করার সময় ইন্টেন্ট ফিল্টার সহায়ক হয়, এটি জাভা কোডে হ্যান্ডেল করা যায় না, বরং AndroidManifest.xml-এ সেটআপ করতে হয়। অ্যান্ড্রয়েডকে জানাতে হয় কোন ধরনের ইন্টেন্ট লঞ্চ করা হচ্ছে, তাই ইন্টেন্ট ফিল্টার ইন্টেন্ট ও অ্যাকশন সম্পর্কে অ্যান্ড্রয়েডকে তথ্য দেয়।
ইন্টেন্ট লঞ্চ করার আগে অ্যান্ড্রয়েড অ্যাকশন টেস্ট, ক্যাটাগরি টেস্ট ও ডেটা টেস্ট করে। এই উদাহরণটি ইন্টেন্ট ফিল্টার ব্যবহার করার পদ্ধতি দেখায়।
ধাপ ১: নতুন প্রজেক্ট তৈরি করুন
অ্যান্ড্রয়েড স্টুডিওতে File → New Project এ যান এবং প্রজেক্ট তৈরির জন্য প্রয়োজনীয় বিবরণ পূরণ করুন।
ধাপ ২: 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>
এখানে একটি বাটন আছে, ক্লিক করলে অ্যাকশনসহ ইন্টেন্ট দেখাবে।
ধাপ ৩: 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 ব্যবহার করে ইন্টেন্ট কল হবে এবং টাইপ message/rfc822 সেট হবে। ইমেইল আইডি ও সাবজেক্ট পাস করা হয়েছে।
ধাপ ৪: AndroidManifest.xml-এ ইন্টেন্ট ফিল্টার যোগ করুন
<?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>
এখানে অ্যাকশন, ক্যাটাগরি ও ডেটা ডিক্লেয়ার করা হয়েছে।
আউটপুট
অ্যাপ রান করুন। ডিফল্ট স্ক্রিন:

বাটনে ক্লিক করলে ইন্টেন্ট চুজার খুলবে:

Gmail সিলেক্ট করলে:

ইন্টেন্ট থেকে ডেটা लेकर জিমেইলে অ্যাপেন্ড করা হয়েছে।