Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of a custom category or action?

I'm reading Beginning Android Application Development by Wei-Mung Lee. I'm confused about custom actions and categories.

Here's some code from one example. The action is a package name. The only time that it's ever referred to ever again is in

Intent i = new Intent( "net.learn2develop.MyBrowser" );

to start an activity. How is it that this action, which is basically a package name, can know to start an activity? Just because it's inside the activity tag?

The same thing with the category tag (different example):

<intent-filter>
   <action android:name=”android.intent.action.VIEW” />
   <action android:name=”net.learn2develop.MyBrowser” />
   <category android:name=”android.intent.category.DEFAULT” />
   <category android:name=”net.learn2develop.Apps” />
   <data android:scheme=”http” />
</intent-filter>

net.learn2develop.Apps is a name that was made up by the author. It really has no meaning, right? What purpose does it serve?

like image 451
ShrimpCrackers Avatar asked Sep 02 '11 23:09

ShrimpCrackers


People also ask

What is the purpose of categories in intents?

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

What is the difference between categories and action?

action : Declares the intent action accepted, in the name attribute. The value must be the literal string value of an action, not the class constant. category: Declares the intent category accepted, in the name attribute. The value must be the literal string value of an action, not the class constant.

What is category in manifest?

You can specify categories in your applications manifest that lets the system know that you application can handle the intent category. For example, by putting a ALTERNATIVE category, other apps in the system know that your app can handle that category without specifically knowing the action name!


2 Answers

How is it that this action, which is basically a package name, can know to start an activity?

Because the <intent-filter> of the activity advertised that it can be started via that action string. BTW, just because it's written like a package name does not mean it has to be a package name (e.g., android.intent.action.VIEW is not a package). The package naming convention is to prevent accidental collisions with other installed apps.

It really has no meaning, right?

Well, it probably meant something to the author, though I couldn't tell you what, exactly.

What purpose does it serve?

In normal Android development, you would not create a custom category. I cannot recall ever seeing one, and I've been doing Android development for quite a while now.

Categories are usually used to distinguish different use cases. For example, perhaps the second-most-popular category besides DEFAULT is BROWSABLE. Activities supporting the VIEW action in the BROWSABLE category become eligible to be used from links in a Web browser. So, if I had an activity for VIEW/BROWSABLE and a MIME type of application/pdf, and the user clicked on a link to a PDF file in a browser, I could be chosen to view the PDF. However, if I lacked BROWSABLE as a category, then I would not be eligible for that link. Usually, an activity would only advertise BROWSABLE if it could retrieve an HTTP URL.

Off the top of my head, I cannot think of a scenario where I'd use a custom category, though.

like image 67
CommonsWare Avatar answered Oct 03 '22 00:10

CommonsWare


What purpose does it serve?

For the vast majority of cases, there is little or no use for setting the category. However, if you wish to export a BroadcastReceiver (ie make it possible for other apps to broadcast to it), but you wish to limit which apps can actually trigger the receiver, one was is to use a private category known only to those apps.

Obviously this must be done programatically:

intent.addCategory('com.super.dooper.thing.my_sectrect_category')

If you put it in the manifest, you are declaring it, and it ceases to be quite so private.

like image 39
Neil Townsend Avatar answered Oct 02 '22 23:10

Neil Townsend