开发者

What is the purpose of a custom category or action?

开发者 https://www.devze.com 2023-04-02 07:59 出处:网络
I\'m reading Beginning Android Application Development by Wei-Mung Lee. I\'m confused about custom actions and categories.

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=”and开发者_如何转开发roid.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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消