On the four major components of android application

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/angelsmiling/article/details/101669739

1 Introduction

android application development is inseparable from the four major components, these four components are: activity, service, brocastrecevier, contentprovider, the following four components will be registered on this, start the life cycle them out.

2. activity

activity is the activity, primarily responsible for displaying user interface.

2.1 Registration

The activity is registered by registering directly in the AndroidManifest.xml file:

<activity
            android:name=".MainActivity"
            android:screenOrientation="portrait" />

2.2 start

promoter activity promoter contains two modes:
1. Start i.e. explicit name explicitly declared specific activity started directly, for example:

 Intent intent = new Intent(this, DeviceListActivity.class);
 startActivity(intent);

2. That is implicitly started by matching action, category data and start-up activities, for example:

Intent intent = new Intent("com.jrmf360.action.ENTER");
startActivity(intent);

ps:

action matching rule
action in the Intent-filter can be set a number of
intent must specify the action or failure to match the action and intent only up to a
time of action and intent intent-filter in the action must be exactly the same (including case) be considered successful match
intent action as long as the intent-filter wherein a match can be successful

2.3 Life Cycle

Here Insert Picture Description
Activity of common life cycle only the following seven:
the onCreate () : represents the Activity is being created, used to initialize the work, such as call setContentView load interface layout resources, and other data required to initialize Activity;
onRestart () : Activity is expressed restart when, under normal circumstances, the current Acitivty never seen again become visible, onRestart will be called;
onStart () : represents the Activity is being started, this time Activity visible but not in the foreground, is still in the background, you can not interact with the user;
onResume () : Indicates activity focus, and this time activity visible and begin activities in the foreground, which is the difference between onStart lies;
onPause () : Indicates activity is being stopped, this time to do some of the stored data, stop animations and so on, but not too time-consuming, because this will affect the display of the new Activity, onPause must be executed, the new Activity onResume will perform;
onStop () : Activity represented about to stop, you can do something a little heavyweight recycling efforts, such as the write-off broadcast receiver, off a network connection, etc., are not the same Too time-consuming;
onDestroy () : Activity is about to be destroyed expressed, this is the last callback Activity lifecycle, often doing recycling work;

ps: If you perform time-consuming operations in the life cycle, prone to ANR, this must pay attention!

3. service

service as a service, it is the presence and activity of the life cycle, but the difference is service not show what interface, you can also say that it is no activity of this interface is not excessive. service provider is a more continuous operation of the role.

3.1 Registration

In fact, registration services and activities in the same way registration, are registered in AndroidManife.xml file, the examples are as follows:

<service android:name=".MyService />

3.2 start

There are two ways to start the service, and are startService bindService

3.2.1 start service

Call the method:

startService(intent);

Features:
Once the service is turned on with the opener has nothing to do;
after opener quit, or long-term service running in the background. Provided that no call stopService (Intent);
opener inside the service method can not be called

3.2.2 bindService

Call the method:

bindService(Intent service, ServiceConnection conn, int flasgs);

Features:
the bind ways to open service, binding service, the caller hung up, hang Service will follow.
Binding service can call methods inside.

3.3 Life Cycle

And activities is different, different ways of starting here will lead to changes in the life cycle.

3.3.1 start service

Here Insert Picture Description

3.3.2 bindService

Here Insert Picture Description

4. brocastrecevier

Radio receivers, after registration, will take the initiative to listen to broadcast messages do not need to start.

4.1 Registration

Registration includes broadcast static registration and dynamic registration mode, static mode actually registered with similar activities and services are registered in AndroidManifest, xml, and then dynamically registered is registered in the code, these two types of registration are big difference.

4.1.1 Static Registration

Radio static is registered in the AndroidManifest.xml file registration, a static registration feature is registered, it will listen to the broadcast, and it's not the life cycle of the life cycle of the program impact.

4.1.2 Dynamic registration

Dynamic registration broadcast is registered in the code, it features is the ability to dynamically control the process of registration and cancellation of broadcasting.

5. ContentProvider

Those containing the content, usage scenario, the above three scenarios is relatively less, it is mainly used in data communication between processes.

Guess you like

Origin blog.csdn.net/angelsmiling/article/details/101669739