Finding Available Actions via Introspection
Sometimes you know just what you want to do, such as display one of your other activities. Sometimes, you have a pretty good idea of what you want to do, such as view the content represented by a , or have the user pick a piece of content of some MIME type. Sometimes you’re lost. All you have is a content , and you don’t really know what you can do with it.
For example, suppose you were creating a common tagging sub‑system for Android, where users could tag pieces of content – contacts, Web URLs, geographic locations, etc. Your sub‑system would hold onto the of the content plus the associated tags, so other sub‑systems could, say, ask for all pieces of content referencing some tag.
That’s all well and good. However, you probably need some sort of maintenance activity, where users could view all their tags and the pieces of content so tagged. This might even serve as a quasi‑bookmark service for items on their phone. The problem is, the user is going to expect to be able to do useful things with the content they find in your sub‑system, such as dial a contact or show a map for a location.
The problem is, you have absolutely no idea what is possible with any given content . You probably can view any of them, but can you edit them? Can you dial them? Since new applications with new types of content could be added by any user at any time, you can’t even assume you know all possible combinations just by looking at the stock applications shipped on all Android devices.
Fortunately, the Android developers thought of this.
Android offers various means by which you can present to your users a set of likely activities to spawn for a given content – even if you have no idea what that content really represents. This chapter explores some of these action introspection tools.
Pick ’Em
Sometimes you know your content represents a collection of some type, such as representing the list of contacts in the stock Android contacts list. In this case, you can let the user pick a contact that your activity can then use (e.g., tag it, dial it).
To do this, you need to create an Intent for the on the target , then start a sub‑activity (via ) to allow the user to pick a piece of content of the specified type. If your callback for this request gets a result code, your data string can be parsed into a representing the chosen piece of content.
For example, take a look at in the sample applications in the Source Code section of http://apress.com. This activity gives you a field for a collection (with pre‑filled in for your convenience), plus a really big Gimme! button:
Upon being clicked, the button creates the on the user‑supplied collection and starts the sub‑activity. When that sub‑activity completes with , the is invoked on the resulting content .
Дата добавления: 2015-05-16; просмотров: 900;