About the difference between service and activity in Android programming

Source: Internet
Author: User

One

Most of the time, the role of service is to "execute" the back-end of the long-run, important task, three indispensable, and the most important reason is the 3rd: to carry out important tasks.

Because when a process starts a service, the priority of the process becomes higher, and the system does not kill the process unless it is very extreme, and it does not have the ability to reverse the activity.

If you do not meet the above three points, please use the service carefully, do not use the service for the reason: this can be resident backstage. From the point of view of resource use, this is very undesirable.

Two

1.Activity and service life cycle is different, activity in which life cycle is controlled by the system, service is programmed, service cannot start itself, and service does not interact with user.

2.A Service is a application component that can perform long-running operations in the background and does not Provide a user interface.

The most important difference is that the service can run in the background and activity cannot.

3.service is a purely logical activity with no interface, then the service can have two states, 1. Run the service in the background after the program exits, 2. Some operations are performed in the background while the program is running.

4. In addition to what is said upstairs, another important use of the service is to implement cross-process invocation, where the concept of binder is more important, aidl is also related to binder, and acitity does not handle the work of cross-process calls.

5.Service runs in the background, similar to a service, and activety is a level similar, but cannot run itself, requires activity or other object calls.

Three

from the point of view of design:
Android activity is designed to be very similar to Web pages, from the page jump through the connection, as well as from the location of the page through the URL, from each page of the independent package, etc. can be seen, it is mainly responsible for interacting with the user.

Service is running in the background, silently provide users with functions, scheduling and coordination, if the ground part of a tree is activity, its huge root is the service. The Android service component is not running in a separate process or thread, and it is running in the main thread of the application as well as other components, and if the service component performs a more time-consuming operation, it causes the main thread to block or feign death, thus unable to respond to the user's actions.

from the point of use:
Service not only can establish two-way connection to activity, provide data and function support for activity, but also can accept intent request in one way, analyze processing and function dispatch of data.

From the role played by :
Activity is more than a single function, mainly to show the application has a number of functions, to help users interact with the application, like a person's face. The service may act as a function dispatcher, can also play a function provider, from the trigger to collect information for analysis and processing, and then update the interface, modify the data or other operations is a function dispatcher, from the input method of choice to consider the service is a function provider. The view component is something that the user can actually see in Android, such as a button, input box, etc. that are inherited from this class, and view is only meaningful in containers such as activity, and in turn
The activity loads the view before it can successfully complete the task of interacting with the user, but the service does not need these fancy things, but waits silently for events to occur or to be sent.

Four

1.Activity is the core concept of Android applications, in short, the activity for the user interaction manager, there is a visual interface, and the difference between service and activity is that he runs in the background, no interface rendering.

2. The small difference between service and activity in the use of intent is that there is a difference in the intent value.

(1)

Jump between activity and service
An activity called mp3listactivity.
A service called Mp3downloadservice
Code Snippets
Intent Intent = new Intent ();
Intent.putextra ("Mp3info", mp3info);
Intent.setclass (This,mp3downloadservice.class);
StartService (Intent);
//--------------------------
You see a jump to a service
//--------------------------
In the service
Startcommand (inent intent,int flags, int startid)
Value code
Mp3info Mp3info = (mp3info) Intent.getserializableextra ("Mp3info");
//-----------------------------------------------------------------


(2)

Jump between activity and activity
An activity called mp3listactivity.
An activity called mp3playactivity.
Key Code Snippets
Intent Intent = new Intent ();
Intent.putextra ("Mp3info", mp3info);
Intent.setclass (This,mp3playactivity.class);
StartActivity (Intent);
//-----------------------------
You see, jump to mp3playactivity.
//-----------------------------
The value code in the OnCreate () Method intent = getintent ();
Mp3info Mp3info = (mp3info) Intent.getserializableextra ("Mp3info");
//------------------------------------------------
Try to understand yourself and get the small difference between service and activity in intent

How do I keep the service running after I close the activity?
1. If your service does not have a binder activity session, the activity Shutdown service is also running in the background
2. How do you start the service, if it is StartService, shut down the calling service thread, the service will not be shut down, if it is bindservice,service and call Bindservice process die, That is, when the process of calling Bindservice is dead, then its bind service will be terminated as well.

http://blog.csdn.net/foreverhuylee/article/details/20372055 original link

About the difference between service and activity in Android programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.