Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Chapter 14 Android components are detailed


May 21, 2021 Android SDK Getting started


Table of contents


Chapter 14 Android components are detailed

So far, we've studied the structure and typical elements of Android applications, including user interface elements and data storage. W ith what you already know, you can start creating your own Android app. B ut before we do, let's go through some of the commonly used Android components -- and that's the main story of today's article. In the next article in this series, we'll explore the SDK sample code.

Android apps include four components: Activity, Service, Content Provider, and Broadcast Receiver. A s long as you create or use any of them, you must add the corresponding elements to the project Manifest. W e've had a lot of talk with Actity before, so I'm not going to waste any more space on this article. N ow let's focus on the other three main application components. It's important to note that I'll also cover some of the other resources you're most likely to use in your application, including fragment and action bar.

1. Service

In Android, a service is the equivalent of a background process. S ervice is typically used for processes that are in progress or that take a long time. I n fact, Service doesn't have a user interface, so they often need to be combined with other components for efficacy, such as teaming up withActivity. T he most typical example is when In an application, Actity launches a service that may upload data to a Web resource while the user is operating. Users can continue to interact with TheActivity, but at the same time the service's operations are unaffected because its execution has been done in the background.

Tip: If you want to perform background processes such as getting Internet data, you don't have to use the Service class. D epending on the actual needs of the application, it may be better to use the internal AsyncTask class in your Own Actity to solve the problem. I t allows background processes to be completely separated from the user interface, but our Activity can still receive the results from AsyncTask and update them to the user interface.

To add a Service component to Manifest, we need to replace the application elements with the following syntax and its elements:

<service android:name=".ServiceClassName" />

You can create a Service class in Eclipse, which is the same process as TheActivity described earlier, but this time chooses Service as a super class. T he differences between service and Actity components, which we've talked about before, are significant. I f we start a service with Actity as a starting point and the user navigates from that Activity to another application, the service will continue to run. As a result, the service has a completely different lifecycle than what we know about Actity, and you need to keep that in mind to make sure your application works.

Other application components can bind to Service and request and receive data from it. I f a binding service is running, it will also go into a stop state when all the components that are bound to it are stopped. A lthough the service is not associated with the user interface of the application, the service that starts with Actity runs on the same thread as it does. H owever, if everyone's service needs to use a lot of processing resources, it's a good idea to create a separate run thread for it. Click here for more instructions on Service from the Android Developer Guide.

2. Content Provider

Another component, TheContent Provider, is used to manage datasets. T he data set referred to here can be simply owned by our application or shared with other applications, capable of querying and modifying the data. I f you create a Content Provider for your application to manage your data, then our UI components, such asActivity, can use that Content Provider, typically using the Content Resolver class to interact with the data. When used by other applications, the Content Provider uses standard methods to access data and interact with structured data sets such as databases.

If you're already familiar with the relationship database, you should be well aware of the data access methods used by Content Provider. T he data is submitted by Content Provider to a series of tables containing rows and columns, each of which contains a separate data value. Therefore, processing data returned through TheContent Provider is very similar to processing database query results.

Although we may sometimes create a dedicated Content Provider program, when we first started developing our first app, we basically used the Content Provider, such as the device calendar or contacts, provided by other developers or the Android system itself. C ontent Provider enables content usage by defining the permissions required by the client app. In order to use TheContent Provider in your application, you need to add permissions to it in Manifest.

Tip: If you just need a structured set of data sources in your application, we don't usually need to create a Content Provider separately. Y ou can create a set of SQLite databases that apply only to our application itself, without the need for the Content Provider class at all. T he only thing we need to create a Content Provider is that people want to copy structured data from our applications to other applications, or you want to use the search framework.

3. Broadcast Receiver

Android systems are able to emit broadcast messages that multiple applications can respond to. Y ou can also develop applications to make these broadcasts, but this possibility is much lower than listening to existing broadcasts, at least for our first application. System notifications include various types of information about the device's hardware, such as battery power, screen shutdown, and whether the charger is plugged into a socket, and so on.

In order to receive broadcast notifications from Android, our app needs to use Broadcast Receiver. A s a typical example, the battery tool updates the display results when the battery level actually changes. In this case, you can use the Service class with Broadcast Receiver to ensure that your application always listens to notifications in the background.

Android calls broadcast notifications intent, which can be used to start Actity. A s an action performed by the system, intent can enable Acty to start or notify. To use Broadcast Receiver, our application must declare it in Manifest, and there is an alternative set of intent filters to indicate what we want to receive:

<receiver android:name=".YourReceiver">
<intent-filter>
<action android:name="android.intent.action.BATTERY_LOW" />
</intent-filter>
</receiver>

With the above code, Broadcast Receiver in our application is able to receive the "battery low" intent. P lease note that we cannot receive all system notifications by declaring action in Manifest. In some cases, you must complete registration to receive notifications, such as using BATTERY_CHANGED Java.

4. Other classes

As you can see, Android components are designed to interact with each other for different applications. J ust as broadcast notifications are available for any app on top of the system, Android also offers a range of other actions that help us accomplish a variety of common tasks in the app, such as dialing a phone number. S imilarly, you can use features provided by other developers to quickly implement scheduled processes, save code development, and help you focus more on the uniqueness of your application. W hen we start an inent, we can set it to return a result to TheActivity in startup -- even if the launched inent is not part of our application. This allows our application to continue running after the required operation is complete.

Other android components of interest include fragment and action bar. Let's briefly explore both:

Fragment

Using fragment may be more efficient than simply usingActivity and layout configurations to define the user interface below each screen in your application. W ith the help of fragment, you can split your user interface into logical parts and even reuse the same parts across multiple screens of your application. I n this way, we can not only save a lot of time to implement the same visual/interaction elements of duplication of effort, but also achieve the effect of modifying a little bit to complete the application of changes to the entire model. Fragment is an integral part of Actity in Android, so each fragment has to adapt to its Own lifecycle.

Action Bar

Action bar is also a key user interface element that we often use in application development. I ts role is to provide our application with a user interface sufficient to work with the entire Android system, which makes it the most familiar element for users of the platform. I n general, the entries displayed in action bar include the user's location in the application and the navigation system between the various parts of the application. To use action bar in Actity, you need to make sure that your class includes actionBarActivity and apply the AppCompat theme to that Action in Manifest.

Summarize

Which Android class and component to use depends on what tasks your app is performing. H owever, after the discussion of the article, I believe that we should be able to help you understand the types and numbers of classes and components, and according to the actual situation to make the right choice. I t's often difficult to decide which component or class to use for a particular feature or functionality, so make sure you have a clear understanding of what they do before you judge. In the next tutorial, we'll explore the Android sample code and the application release process.