How to Create/Disable Intent-Filter Programmatically?

How to Create/Disable Intent-Filter Programmatically?

I have three activity and three Intent Filters for them in the Android Manifest.

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>                 
    </activity>
    <activity 
        android:name=".firstActivity"
        android:theme="@style/AppTheme" 
        android:label="@string/first">
        <intent-filter>
            <action android:name="android.intent.action.SEND" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="image/*" />
        </intent-filter>  
   </activity>
    <activity 
        android:name=".secondActivity"
        android:theme="@style/AppTheme" 
        android:label="@string/second">
        <intent-filter>
            <action android:name="android.intent.action.SEND" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="image/*" />
        </intent-filter>  
   </activity>
   <activity 
        android:name=".thirdActivity"
        android:theme="@style/AppTheme" 
        android:label="@string/third">
        <intent-filter>
            <action android:name="android.intent.action.SEND" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="image/*" />
        </intent-filter>  
   </activity ></application>

How can I disable the intent filters programmatically depending on some options? Or how can I create new intent filters in code?

Thanks.

4 Answers

You can neither enable, disable, or create <intent-filter>s programmatically.

However, in your case, you only have one <intent-filter> per component. In that case, you can enable and disable the component programmatically, via PackageManager and setComponentEnabledSetting(). In your case, enabling or disabling the activity would have the same basic effect as enabling or disabling its <intent-filter>.

7

If your Activity had multiple intent-filters then you could disable a specific intent-filter by creating an activity-alias with the intent-filter you want to disable and disable just the Activity alias. See:

An intent filter is an instance of the IntentFilter class. However, since the Android system must know about the capabilities of a component before it can launch that component, intent filters are generally not set up in Java code, but in the application's manifest file (AndroidManifest.xml) as elements. (The one exception would be filters for broadcast receivers that are registered dynamically by calling Context.registerReceiver(); they are directly created as IntentFilter objects.)

source:

Also, see this:

What I did is registerReciever and

registerReceiver(this.broadcastReceiver, new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));

and unregister the broadcastReciever

if (this.broadcastReceiver != null) {
        unregisterReceiver(this.broadcastReceiver);
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Marcus Vance
Author

Marcus Vance

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.