Intent Filter
Intent Filter
It is a powerful way to connect different applications together hence allowing better user
experience. They take care of intent resolution to match activities, services and broadcast receiver.
They are declared in AndroidManifest.xml. An intent filter is an instant of the intent filter class. It
declares the capability of its parent component.
Syntax
</intent_filter>
Attributes
android:icon: It represent the parent activity, service or broadcast receiver when that component is
existing to the user as having the potential described by the filter. This attribute must be set as a
reference to a drawable resource containing the image definition. The defaulting value is the icon set
by the parent component’s icon attribute. If the aren’t do not specify an icon, the default is the icon
set by the <application> element.
android:label: A user-readable label for the parent component. This label is used, when the
component is offered to the user as having the capability described by the filter. The label should be
set as a reference to the string resource so that it could be localized by other strings in the user
interface. The default value is the label set by the parent component. If the parent dose not specify a
label, the default is the label set by the <application> element’s label attribute.
Program
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/sendMail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="150dp"
android:layout_marginTop="230dp"
android:text="Send Mail" />
</LinearLayout>
package com.example.intentfilter;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnSend = (Button)findViewById(R.id.sendMail);
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent si = new Intent(Intent.ACTION_SEND);
si.setType("message/rfc822");
si.putExtra(Intent.EXTRA_EMAIL, new String[]
{"[email protected]"});
si.putExtra(Intent.EXTRA_SUBJECT, "Welcome to Intent
Filters");
si.putExtra(Intent.EXTRA_TEXT, "Hi Guest, Welcome to Intent
Filters...!!!");
startActivity(Intent.createChooser(si,"Choose Mail App"));
}
});
}
}
<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/Theme.Intentfilter">
<activity
android:name=".MainActivity"
android:exported="true">
<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>