Note Activities might have certain capabilities that restrict them or enable them such as whether you can embed them in a parent activity These types of activity characteristics are declared through categories

Let us take a quick look at some predefined Android categories and how you use them (see Table 3-2).

Table 3-2. Activity Categories and Their Descriptions

Category Name

Description

CATEGORY_DEFAULT

CATEGORY_BROWSABLE

CATEGORY_TAB CATEGORY_ALTERNATIVE

CATEGORY_SELECTED_ALTERNATIVE

CATEGORY_LAUNCHER CATEGORY_HOME

CATEGORY_PREFERENCE

CATEGORY_GADGET CATEGORY_TEST CATEGORY EMBED

An activity can declare itself as a DEFAULT activity to operate on a certain aspect of data such as type, scheme, and so on.

An activity can declare itself as BROWSABLE by promising the browser that it will not violate browser-security considerations when started.

An activity of this type is embeddable in a tabbed parent activity.

An activity can declare itself as an ALTERNATIVE activity for a certain type of data that you are viewing. These items normally show up as part of the options menu when you are looking at that document. For example, print view is considered an alternative to regular view.

An activity can declare itself as an ALTERNATIVE activity for a certain type of data. This is similar to listing a series of possible editors for a text document or an HTML document.

Assigning this category to an activity will allow it to be listed on the launcher screen.

An activity of this type will be the home screen. Typically, there should be only one activity of this type. If there are more, the system will prompt you to pick one.

This activity identifies an activity as a preference activity, so it will be shown as part of the preferences screen.

An activity of this type is embeddable in a parent activity.

A test activity.

This category has been superseded by the GADGET category, but it's been kept for backward compatibility.

You can read the details of these activity categories at the following Android SDK URL for the Intent class: http://code.google.com/android/reference/android/content/Intent. html#CATEGORY_ALTERNATIVE.

When you use an intent to start an activity, you can specify the kind of activity to choose by specifying a category. Or you can search for activities that match a certain category. Here is an example to retrieve a set of main activities that match the category of CATEGORY SAMPLE CODE:

Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); mainIntent.addCategory(Intent.CATEGORY_SAMPLE_CODE); PackageManager pm = getPackageManager();

List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);

PackageManager is a key class that allows you to discover activities that match certain intents without invoking them. You can cycle through the received activities and invoke them as you see fit, based on the ResolveInfo API.

Following the same logic, you can also get a list of all launchable applications by populating an intent with a category of CATEGORY_LAUNCHER:

//Get me all launchable applications

Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

List mApps = getPackageManager().queryIntentActivities(mainIntent, 0);

In fact, we can do better. Let's start an activity based on the preceding intent category CATEGORY_LAUNCHER:

public static void invokeAMainApp(Activity activity) {

Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);

mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

activity.startActivity(mainIntent);

More than one activity will match the intent, so which activity will Android pick? To resolve this, Android presents a "Complete action using" dialog that lists all the possible activities so that you can choose one to run.

Here is another example of using an intent to go to a home page:

//Go to home screen

Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);

mainIntent.addCategory(Intent.CATEGORY_HOME);

startActivity(mainIntent);

If you don't want to use Android's default home page, you can write your own and declare that activity to be of category HOME. In that case, the preceding code will give you an option to open your home activity because more than one home activity is registered now:

//Replace the home screen with yours <intent-filter>

<action android:value="android.intent.action.MAIN" /> <category android:value="android.intent.category.HOME"/> <category android:value="android.intent.category.DEFAULT" /> </intent-filter>

0 0

Post a comment