2

I have an application that contains an activity that is spawned by an intent filter (in this case android.hardware.usb.action.USB_DEVICE_ATTACHED), what it should do (and does) is the following : when the application in question isn't running and the user attaches an USB device that matches the applications filter (e.g. attached device matches the type the application supports), a popup will appear requesting my application (or others with a similar filter) to be opened.

However: I only want this popup to appear/behaviour to occur on Android devices that are of a specific build version (for instance : only run this activity on Android M and above, not for Android API levels 5 or lower).

Is such a thing possible?

1 Answer 1

3

To have the activity itself be available only on certain API levels, create a boolean resource, with different values tied to different API levels. For example, you could create a res/values/bools.xml file, defining a boolean resource, with a name of is_honeycomb, and a value of false. You could then create a res/values-v11/bools.xml file, defining another boolean resource, with the same name (is_honeycomb), and a value of true. The more complex your scenario (e.g., "I only want API levels that are prime numbers"), the more editions of your resource you may need to define.

Then, on the <activity> element have android:enabled="@bool/is_honeycomb" (substituting in your chosen resource name), and it will only be enabled on the API levels for which the resource is true.

Alas, <intent-filter> does not have android:enabled. You might be able to work around that by using an <activity-alias> for your sometimes-available <intent-filter>, then enable/disable that alias, though I don't think I have tried this.

1
  • Works beautifully, exactly what I was looking for, and as a solution it's quite elegant as it can be configured using the resource configurations. Thanks! Nov 15, 2015 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.