Development by Davis: “Debunking The Oatmeal and the perception of Linux as difficult to use” plus 3 more
|
- Debunking The Oatmeal and the perception of Linux as difficult to use
- Two reasons I love blogging: Helping out great communities
- The first FOSDEM Legal Issues DevRoom
- Share With Intents
Posted: 10 Feb 2012 04:00 AM PST
read more |
Posted: 10 Feb 2012 02:00 AM PST
There have been two great examples of this type of behaviour on my blog over the past week. read more |
Posted: 09 Feb 2012 10:20 AM PST
The idea of a Legal DevRoom being untested, FOSDEM's organizers gave us a single day and a relatively small room. Our basic goal was to read more |
Posted: 09 Feb 2012 08:06 AM PST
[This post is by Alexander Lucas, an Android Developer Advocate bent on saving the world 5 minutes. —Tim Bray] [Please join the discussion on Google+.] Intents are awesome. They are my favorite feature of Android development. They make all sorts of stuff easier. Want to scan a barcode? In the olden platforms, if you were lucky, this involved time and effort finding and comparing barcode-scanning libraries that handled as much as possible of camera interaction, image processing, an internal database of barcode formats, and UI cues to the user of what was going on. If you weren't lucky, it was a few months of research & haphazard coding to figure out how to do that yourself. On Android, it's a declaration to the system that you would like to scan a barcode.
See that? That's nothing. That's 5 minutes of coding, 3 of which were just to look up the name of the result you wanted to pull. And that was made possible because the Barcode Scanner application is designed to be able to scan barcodes for whatever other applications may need it.More important, our app is completely decoupled from the BarcodeScanner app. There's no integration- in fact, neither application is checking to verify that the other exists. If the user preferred, they could remove "Barcode Scanner" and replace it with a competing app. As long as that app supported the same intent, functionality would remain the same. This decoupling is important. It's the easy way. It's the lazy way. It's the Android way. Sharing Data Using IntentsOne of the most inherently useful Android intents is the Share intent. You can let the user share data to any service they want, without writing the sharing code yourself, simply by creating a share intent.
... and starting it with a chooser:
With these 5 lines of code, you get to bypass authenticating, credential storage/management, web API interaction via http posts, all sorts of things. Where by "bypass", I mean "have something else take care of." Like the barcode scanning intent, all you really had to do was declare that you have something you'd like to share, and let the user choose from a list of takers. You're not limited to sending text, either. Here's how you'd create an intent to share an image:
Note that just by using setType() to set a MIME type, you've filtered down the list of apps to those that will know what to do with an image file.Intents over IntegrationThink about this for a second. By making the simple assumption that any user of any service (Task Manager, Social Network, Photo sharing site) already has some app on their phone that can share to that service, you can leverage the code that they've already written. This has several awesome implications:
Avoid One-Off IntegrationsFor each pro of the Intent approach, integrating support to post to these services one-at-a-time has a corresponding con.
Ice Cream Sandwich makes it even easierWith the release of ICS, a useful tool for sharing called ShareActionProvider was added to the framework, making the sharing of data across Android applications even easier. ShareActionProviders let you populate lists of custom views representing ACTION_SEND targets, facilitating (for instance) adding a "share" menu to the ActionBar, and connecting it to whatever data the user might want to send.Doing so is pretty easy. Configure the menu items in your Activity's onCreateOptionsMenu method, like so:
Note that you can specify a history file, which will adapt the ordering of share targets based on past user choices. One shared history file can be used throughout an application, or different history files can be used within the same application, if you want to use a separate history based on what kind of data the user wants to share. In the above example, a custom history file is used. If you wish to use the default history for the application, you can omit that line entirely. This will help optimize for an important feature of the ShareActionProvider: The user's most common ways to share float to the top of the drop-down, with the least used ones disappearing below the fold of the "See More" button. The most commonly selected app will even become a shortcut right next to the dropdown, for easy one-click access! You'll also need to define a custom menu item in XML. Here's an example from the ActionBar Dev Guide.
And with that, you can have an easy sharing dropdown that will look like the screenshot here. Note that you get the nice standard three-dots-two-lines "Share" glyph for free.Remember: Smart and EasyThe share intent is the preferred method of sharing throughout the Android ecosystem. It's how you share images from Gallery, links from the browser, and apps from Android Market. Intents are the easiest path to writing flexible applications that can participate in a rapidly expanding ecosystem, but they're also the smart path to writing applications that will stay relevant to your users, letting them share their data to any service they want, no matter how often their preferences change over time. So take a step back and stop worrying about if your user wants to tweet, digg, post, email, im, mms, bluetooth, NFC, foo, bar or baz something. Just remember that they want to share it. Android can take it from there. |
You are subscribed to email updates from Developers by Davis To stop receiving these emails, you may unsubscribe now. | Email delivery powered by Google |
Google Inc., 20 West Kinzie, Chicago IL USA 60610 |
No hay comentarios:
Publicar un comentario