Intent Filters for Plug-Ins and Extensibility in Android

Having used Intent Filters to declare the actions your Activities can perform on different types of data, it stands to reason that applications can also query to fi nd which actions are available to be performed on a particular piece of data

Android provides a plug-in model that lets your applications take advantage of functionality, provided anonymously from your own or third-party application components you haven’t yet conceived of, without your having to modify or recompile your projects

Supplying Anonymous Actions to Applications

The Intent Filter describes the action it performs and the data upon which it can be performed. The latter will be used during the intent-resolution process to determine when this action should be available. The category tag must be either ALTERNATIVE or SELECTED_ALTERNATIVE, or both. The android:label attribute should be a human-readable label that describes the action

Advertising supported Activity actions

<activity android:name=”.NostromoController”>
 <intent-filter
 android:label=”@string/Nuke_From_Orbit”>
 <action android:name=”com.pad.nostromo.NUKE_FROM_ORBIT”/>
 <data android:mimeType=”vnd.moonbase.cursor.item/*”/>
 <category android:name=”android.intent.category.ALTERNATIVE”/>
 <category 
 android:name=”android.intent.category.SELECTED_ALTERNATIVE” 
 />
 </intent-filter>
</activity>

Generating a list of possible actions to be performed on specific data

PackageManager packageManager = getPackageManager();
// Create the intent used to resolve which actions
// should appear in the menu.
Intent intent = new Intent();
intent.setData(MoonBaseProvider.CONTENT_URI);
intent.addCategory(Intent.CATEGORY_SELECTED_ALTERNATIVE);
// Specify flags. In this case, to return only filters
// with the default category.
int flags = PackageManager.MATCH_DEFAULT_ONLY;
// Generate the list
List<ResolveInfo> actions;
actions = packageManager.queryIntentActivities(intent, flags);
// Extract the list of action names
ArrayList<String> labels = new ArrayList<String>();
Resources r = getResources();
for (ResolveInfo action : actions )
 labels.add(r.getString(action.labelRes));

Dynamic Menu population from advertised actions

@Override
public boolean onCreateOptionsMenu(Menu menu) {
 super.onCreateOptionsMenu(menu);
 // Create the intent used to resolve which actions
 // should appear in the menu.
 Intent intent = new Intent();
 intent.setData(MoonBaseProvider.CONTENT_URI);
 intent.addCategory(Intent.CATEGORY_SELECTED_ALTERNATIVE);
 // Normal menu options to let you set a group and ID
// values for the menu items you’re adding.
 int menuGroup = 0;
 int menuItemId = 0;
 int menuItemOrder = Menu.NONE;
 // Provide the name of the component that’s calling
 // the action -- generally the current Activity.
 ComponentName caller = getComponentName();
 // Define intents that should be added first.
 Intent[] specificIntents = null;
 // The menu items created from the previous Intents
 // will populate this array.
 MenuItem[] outSpecificItems = null;
 // Set any optional flags.
 int flags = Menu.FLAG_APPEND_TO_GROUP;
 // Populate the menu
 menu.addIntentOptions(menuGroup,
 menuItemId,
 menuItemOrder,
 caller,
 specificIntents,
 intent,
 flags,
 outSpecificItems);
 return true;
}

Leave a Comment