Another way you can make content-rich applications more readily available to users is with the use of live folders. Introduced in Android 1.5 (API Level 3), a live folder is a special type of folder that the user can place in various areas such as the Home screen and, when clicked, displays its content by querying an application that acts as a content provider. Each piece of data in the folder can be paired with an intent. You could also think of it as a folder of shortcuts into your application. For example, a music application might allow the user to create live folders for favorite music. Similarly, a to-do list application might include support for a live folder of the day’s tasks. Finally, a game might have a live folder for saved game points. When the user clicks on an item, the application launches to play the appropriate song, show the appropriate to-do list item, or start the game at that save point. Applications can support live folders with different types of content— it all depends on the content the application has to expose.
Let’s return to the example of the African field notes application and update it so that users can create live folders with field note titles. Clicking on a specific field note launches the application with an action VIEW for the full field note contents (again, by acting as a content type handler, as discussed earlier in this chapter).
Creating Live Folders
To enable in-application live folder creation within your application, you need
Now let’s look at some of these requirements in more detail.
Creating a Live Folder Activity
An application that supports live folders must include a live folder creation activity. This activity is launched anytime the application reacts to the intent action ACTION_CREATE_LIVE_FOLDER. The live folder creation activity is responsible for one thing: responding with a specific instance of a live folder configuration. If you recall how the startActivityForResult() method works, this is exactly how the live folder creation activity is called. The activity needs to retrieve the incoming intent that performed the live folder creation request, craft a new live folder (as an Intent object with suitable extras to specify the live folder configuration details) and set the Activity result using the setResult() method. The setResult() method parameters can be used to communicate whether or not the live folder creation was successful as the resultCode parameter and pass back the specific instance of the live folder as the accompanying result Intent data parameter.
Sounds a tad complex, eh? Well, let’s look at a specific example. Here is the implementation of the live folder creation activity for the field notes application:import android.app.Activity;
As you can see, the SimpleLiveFolderCreateActivity has a very short lifespan. It waits for ACTION_CREATE_LIVE_FOLDER requests and then crafts the appropriate Intent object to return as part of the activity result. The most important code in this activity is the code that creates the new intent called resultIntent. This Intent object contains all the configuration details for the new live folder instance. The setData() method is used to supply the live Uri (the Uri to query to fill the folder with data).
Several extras are set to provide the live folder instance with a label and icon, as well as specify the display mode of the live folder. Live folders have several canned display modes: The DISPLAY_MODE_LIST value causes all live folder content to display in ListView control (ideal for text content) and the DISPLAY_MODE_GRID displays live folder content in a GridView control—more appropriate if the live folder contents are graphics. Finally, the base Intent object for each live folder item is set. In this case, the base intent has an action type of VIEW, as you might expect, and therefore is compatible with the content type handler technique. For more information on the configuration details that can be applied to a live folder, see the Android SDK documentation for the android.provider.LiveFolders package.
Handling Live Folder Content Provider Queries
Each time the user opens the live folder, the system performs a content provider query. Therefore, the application’s content provider interface needs to be updated to handle queries to fill the live folder with data. As with search suggestions, you need to define a projection in order to map the content provider data columns to those that the live folder expects to use to fill the list or grid (depending on the display mode) within the folder. For example, the following code defines a project to map the field notes’ unique identifiers and titles to the ID and name fields for the live folder items:private static final HashMap<String, String>
Whenever the live folder is opened by the user, the system executes a query on the Uri provided as part of the live folder configuration. Don’t forget to define the live Uri address and register it in the content provider’s UriMatcher object (using the addURI() method). For example, the field notes application used the Uri:content:// com.androidbook.simplelivefolder.
By providing a special live folder Uri for the content provider queries, you can simply update the content provider’s query method to handle the specialized query, including building the projection, performing the appropriate query, and returning the results for display in the live folder. Let’s take a closer look at the field notes content provider query() method:@Override
This query() method implementation handles both regular content queries and special live folder queries (those that come in with the live Uri).When the live folder query occurs, we simply use the handy setProjectionMap() method of the QueryBuilder object to set and execute the query as normal.
Configuring the Android Manifest File for Live Folders
Finally, the live folder Activity class needs to be registered within the application Android manifest file with an intent filter with the CREATE_LIVE_FOLDER action. For example, here is an excerpt from the field notes Android manifest file that does just that:<activity
This type of Activity registration should look familiar. The SimpleLiveFolderCreateActivity class is responsible for handling the CREATE_LIVE_FOLDER intent (as dictated by the intent filter).You can also set the live folder’s text label and icon using the attributes for the creation activity.
Installing a Live Folder
After the application is capable of handling the creation of a live folder, your development work is done. As a user, you can install a live folder on the Home screen using the following steps:
Installing a live folder on the Home screen.
A live folder on the Home screen.
Choosing a specific field note from the live folder (left) launches the application to display its content (right).
Setting Up Your Android Development Environment
Writing Your First Android Application
Understanding The Anatomy Of An Android Application
Defining Your Application Using The Android Manifest File
Managing Application Resources
Exploring User Interface Screen Elements
Designing User Interfaces With Layouts
Drawing And Working With Animation
Using Android Data And Storage Apis
Sharing Data Between Applications With Content Providers
Using Android Networking Apis
Using Android Web Apis
Using Location-based Services (lbs) Apis
Using Android Multimedia Apis
Using Android Telephony Apis
Using Android 3d Graphics With Opengl Es
Using The Android Ndk
Using Android’s Optional Hardware Apis
Working With Notifications
Working With Services
Extending Android Application Reach
Managing User Accounts And Synchronizing User Data
Handling Advanced User Input
Targeting Different Device Configurations And Languages
The Mobile Software Development Process
Designing And Developing Bulletproof Android Applications
Testing Android Applications
Selling Your Android Application
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.