17

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.

Minas Mina
  • 2,058
  • 3
  • 21
  • 35
mamutido
  • 191
  • 1
  • 1
  • 8

4 Answers4

35

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

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    What about IntentFilter.create and Context.registerResolver ? http://developer.android.com/reference/android/content/IntentFilter.html#create – Jonathan Muller Feb 16 '15 at 13:17
  • @Koren: There is no `registerResolver()` method. You may be thinking of `registerReceiver()`. This is possible for `BroadcastReceivers`, but the OP is inquiring about activities. Also, `registerReceiver()` only works while your process is running, whereas an `` works even when your process is not running. – CommonsWare Feb 16 '15 at 13:19
  • Yes I was talking about registerReceiver sorry – Jonathan Muller Feb 16 '15 at 13:24
  • I just tried to disable an activity via PackageManager, but the intent-filter stays alive. Any more suggestions? – Grisgram Oct 03 '16 at 09:48
  • @Grisgram: What you describe is not possible, at least by how I would define the terms that you used. You may wish to ask a separate Stack Overflow question, where you provide a [mcve] and explain how you have determined that you both *successfully* disabled the activity *and* the `` "stays alive". – CommonsWare Oct 03 '16 at 10:45
  • It works - apologies - it was my own mistake and I commented a bit too fast here :) - sorry. It's all good. Works as described. – Grisgram Oct 04 '16 at 09:45
  • If you have more than one intent-filter per activity you could use activity-alias to disable them individually. Check this answer: http://stackoverflow.com/a/10403074/1954655 – aglour Feb 10 '17 at 14:36
11

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: https://developer.android.com/guide/topics/manifest/activity-alias-element.html

Piotr Zawadzki
  • 1,678
  • 1
  • 18
  • 24
7

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: http://developer.android.com/guide/components/intents-filters.html

Also, see this: https://stackoverflow.com/a/10403074/832776

Community
  • 1
  • 1
Oleg Vaskevich
  • 12,444
  • 6
  • 63
  • 80
1

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);
    }