What are the purpose of intent categories?

2020-02-17 08:32发布

Could someone please explain me the purpose of Intent categories? When should I make my own and so on? The only thing thats written about Intent categories in my book is that they can group intents?.

1条回答
SAY GOODBYE
2楼-- · 2020-02-17 08:50

The category's alone are useless, they are used to describe a possible target for an "implicit intent" in an intent-filter.

When you know which class/activity you want to launch and use startActivity() or startActivityForResult(), it's called an "explicit intent".

Here's an analogy for how implicit intents work:

Imagine all your applications sitting in a big room and doing nothing. Then, another application, let's say Dropbox, needs someone to open a PDF file. The Dropbox app goes to the system and says "Hey, somebody needs to open this PDF file..." (This is sending the implicit intent).

The system now goes to the room and yells "Which one of you can display a PDF file?". The applications that can stand up and the system sees them (these applications have an activity with a matching intent category).

It then offers you a dialog, in which you can choose one of the applications: Complete action using


If you want to make some of your Activity/BroadcastReceivers/Services available outside of your applications bounds, you can use the Android Manifest to declare an "intent filter" to it, so it gets opened when the system or an app launches an "implicit intent" that matches.

You do this (for example) for the Activity which should be opened from the launcher:

<activity android:name=".SomeActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

This listens to the ACTION_MAIN-action triggered by Androids Launcher (CATEGORY_LAUNCHER).

You have two child-elements in your "intent filter":

  1. The action. This specifies what action the "intent filter" should listen to.
  2. One or multiple categorys. This specifies, how the activity should be called.

One of the categorys can be (for example) android.intent.category.DEFAULT, which tells the Activity to be launched normally in full-screen-mode. The android.intent.category.TAB-category for example declares this activity as a tab in a TabActivity, so it can only be opened as a Tab.

Another example would be adding the android.intent.category.PREFERENCE-category, which would declare the activity as your Settings-Activity.


Declaring your own categorys is neither possible nor necessary.

Further more you'll need to understand that those events are triggered by the System/Another App and you can only specify if and how you want to react when they are triggered.

查看更多
登录 后发表回答