Preference Settings allow the user to select basic settings for an app. It’s a way of customising the app to suite the user.
This tutorial will show you how you can create your own custom Preference Settings for your apps.
We’ll be using fragments and Headers so this tutorial is suitable for apps developed for devices running Android 3.0 and up.
There are four parts to the Preference Framework. We’ll cover all of them:
Each Preference appears as an item in a list. The user is able to modify these preference settings.
Well, it’s sort of about context menus but different.
It’s all about using the contextual action mode. This lets the user do stuff with the items that they have selected.
Enabling the contextual action mode, displays a Contextual Action Bar at the top of the screen. You can then place action items, which are like menu options, on this bar.
The user selects items from a list for example, chooses an action item and the action linked to that action item is performed on all of the selected items.
Say you had a list of countries and you want the user to be able to select a couple of them to add to a database. This is where you could use the contextual action mode.
It enables the user to select one or more of the countries in the list. They can then select a Save action item, for example, and all the selected country items will be saved in the database.
We’ll show you how to:
This tutorial will show you how to:
You’ve probably used intents to start activities. You can also use intents to broadcast messages.
The broadcast intent carries information about who should receive the message and what should be done with it.
Broadcast Receivers can receive these messages.
This broadcast mechanism runs in the background. The user is not aware of it as Broadcast receivers don’t have a User Interface.
You can also use Broadcast receivers to do small bits of work which must complete within 5 seconds. If they don’t, then an Application Not Responding dialog appears.
Usually you would use a receiver to start other components like Services. You can also send a notification from the receiver.
Activities are application components that we, mostly, see as the screen. It displays images, text, buttons, etc. that we interact with. Some activities don’t display anything.
Apps usually have a number of activities. One is seen as the main activity. This is the first screen we see when the app starts.
You should keep your resources, like images, strings, values, etc. separate from your code.
Some of the folders in the res directory
Our tutorial app uses a Service to play a sound clip.
We start a Service when the app starts. The Service will run as long as the app is alive.
We need to bind to the Service to start and stop playing a sound clip. We can leave the clip playing in the Service while it is running. It will continue to play even if the activity is paused or destroyed.
So what happens when we bind to a Service?
It’s unlikely that the system will kill a foreground Service.
Typically we’d use foreground Services for work that the user is aware of, like playing music.
When we use a foreground Service, we have to send a notification to: