Android service usage (IPC, AIDL)

Source: Internet
Author: User

The main functions of the service are as follows:

A. perform an operation that requires a long time to run. This operation does not interact with users, such as network downloads, large file I/O, and complex computing. B. data communication within or between applications. Each Android Application runs in its own dalvik Virtual Machine. One application cannot access the memory information of other applications, therefore, Android introduces Content providers to share data between different applications, and BroadcastReceiver broadcasts information to different applications. However, Content providers are more used for data sharing, broadcastReceiver information is received by all applications, which consumes a lot of system resources. Service is also required for dynamic interaction between two applications. First, services can be used as follows: 1. services in a single application 2. Services and Communications in multiple applications (Note: multiple applications) 1. services in a single application. It can be discussed in two cases: 1) Calling startService in Activity (the most common method of use). This service cannot interact with the outside world, that is, you cannot obtain the related properties and operations of the service. Service example public class MyService extends Service {@ Override public void onCreate () {super. onCreate (); Toast. makeText (this, "Service Create", Toast. LENGTH_SHORT ). show () ;}@ Override public void onDestroy () {Toast. makeText (this, "Service Destroty", Toast. LENGTH_SHORT ). show (); super. onDestroy () ;}@ Override public int onStartCommand (Intent intent, int flags, int startId) {Toast. makeText (this, "Ser Vice Start ", Toast. LENGTH_SHORT ). show (); return super. onStartCommand (intent, flags, startId) ;}} in this case, we put most of the work to be done in onCreate or onStartCommand. However, note: this service does not have a separate thread and is still the main thread. Therefore, when you perform various time-consuming operations in it, ANR (limits the response time of the Activity and BroadcastReceiver (the response time of the Activity to the event cannot exceed 5 seconds, and that of BroadcastReceiver cannot exceed 10 seconds )). Generally, it is best to open another thread in the service for time-consuming operations. Therefore, IntentService is recommended. IntentService re-opens a thread to process the tasks to be completed. Life cycle: start the service through startService. if the service is not started, the onCreate function is executed first (if the service is started, the onStartCommand function is not executed), and then the onStartCommand function is executed. It can be seen that multiple calls to startService and passing in the same parameter will not start multiple services (the onStartCommand function will execute multiple times). Therefore, you only need to call the stopService or stopSelf function once to stop the service; we can put the service processing logic into the onStartCommand function. The Service continues to run. After the program exits, the service will not stop until the stopService or stopSelf function is called. Of course, the Service may be recycled by the system. For the onStartCommand return value, if START_STICKY is returned, it indicates that the service is started or stopped through an explicit call. If START_NOT_STICKY orSTART_REDELIVER_INTENT is returned, the service is running only when a request is sent for processing. Use Cases: Because this method does not allow convenient and dynamic interaction with the outside, it is suitable for backend services, such as network downloads (IntentService is recommended for users to import URLs to the Service through Intent ). 2) start the Service (Dynamic Interaction Service) through multiple bindservices. If multiple threads in an application need to communicate with the Service, we can use bindService. In this way, we can operate and access the properties in the Service. Use Cases: In-app communication, such as a music player, controls the playing, pause, and stop of the player in the service, and controls the player through service operations in the Activity. 2. Communication between applications (this is the communication between different applications) 1), Using a Messenger: http://developer.android.com/guide/components/bound-services.html#Messenger use scenario: when only two applications need to communicate with each other, we use the Messenger class instead of AIDL. 2) AIDL Android uses AIDL to complete inter-process communication (IPC). AIDL defines the ition Language for Android Interface throughout the process. AIDL is required only when the service needs to accept multi-threaded requests from different applications. If the request in the same application is implemented using a Binder, see the service for interaction in the application; if you only use inter-Application Communication instead of multi-thread processing, you can also use AIDL in both cases. The use of AIDL varies between local and remote processes. both local and remote processes are executed in the called thread during internal calls, A remote process is called through a thread pool maintained by the system in the Service process. Therefore, it may be called by unknown threads at the same time. Pay attention to thread security issues. Use Cases: when we have multiple clients (applications) that need to communicate with the server, that is, when the service needs to accept multi-threaded requests from different applications, we need to use AIDL.

Related Article

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.