How to create/disable intent-filter programmatically?

mamutido picture mamutido · Jan 3, 2013 · Viewed 18k times · Source

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.

Answer

CommonsWare picture CommonsWare · Jan 3, 2013

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>.