On the interaction between Android activity and service _android

Source: Internet
Author: User

The ability to implement updates to download progress

1. Interactive via broadcast

The server side will present the download progress, broadcast the way out, the client side registers this broadcast listener, when obtains this broadcast, the broadcast current download progress resolves and updates to the interface.

Pros and Cons Analysis:

The interactive operation of activity and service through broadcasting is simple and easy to implement, and can be used in simple level. But the disadvantage is also very obvious, send broadcast by system restriction. System-level broadcasts are given priority, and in certain cases our customized broadcasts may be delayed. It is not possible to handle long time consuming operations in broadcast receivers, otherwise the system will be ANR, that is, the application is unresponsive

2. Sharing file interaction

The shared files mentioned here refer to the activity and service using the same file to deliver the data. We use sharedpreferences to implement the sharing, of course, we can use other IO methods to implement the interaction in this way, we need to note that for the file read and write, the same time can only read one side of the writing, can not write both sides

Pros and Cons Analysis:

The interaction between activity and service in this way can be said to be convenient, like using a pipe, one to write to the other, and one to read out. However, this approach is also flawed, writing data is more complex and the volume of data is large, it can lead to write and read data inconsistent error. At the same time, this operation will be more time-consuming because it passes through a relay station.

3. Messenger Interaction (Messenger interaction) Handler

Messenger translates to refer to the Messenger, which refers to a handler object that others can send messages to (using the Mmessenger.send message msg method). This class allows message-based communication across processes, creates a messenger using handler on the server, and communicates with the server as long as the client obtains the Messenger object for that server. In other words, we can think of messenger as client-side and server-side transmitter so that we can communicate

Pros and Cons Analysis:

Through Messenger to achieve activity and service interaction, a little bit deeper we can know, in fact, Messenger is also through Aidl to achieve. For the first two implementations, the messenger approach is generally relatively easy to understand, and it is a reason to use handler and thread communication in peace.

4. Custom interface Interaction in server

Customize an interface that has an empty method to get the current download progress. The server side inherits from the binder with a class and implements the interface, covering how to get the current download progress. The client side obtains the object of the class through the serviceconnection, enabling it to use the method that obtains the current download progress, and finally realizes real-time interaction.

Public interface Icountservice {public 
  int getcurrentload (); 
public class Downloadservice extends Service implements icountservice{ 
private Servicebinder servicebinder = new Serv Icebinder ();  
public class Servicebinder extends Binder implements icountservice{ 
  @Override public 
  int Getcurrentload () { 
    log.i (TAG, "Servicebinder getcurrentload () ... i=:" +i); 
    return i; 
  }   
} 
@Override public 
int Getcurrentload () {return 
  0; 
} 
}
@Override public 
IBinder onbind (Intent Intent) { 
  log.i (TAG, "downloadservice.onbind () ..."); 
  return servicebinder; 

The client-side timer starts to get the download progress 1 seconds after the Bindservice () is completed, and gets the method directly through the int curload = Icountservice.getcurrentload () The Getcurrentload () method here is the method in the Downloadservice inner class Servicebinder. The client side updates the download progress obtained to the interface and updates the progress bar.

Pros and Cons Analysis:

This method is simple, practical and extensible, but it also has some drawbacks, such as the need to delay some of the data to start getting the server side, so that you cannot fully implement a zero-start synchronization update. According to the above, it is practical to realize the interaction between activity and service through the custom interface. Applies to communication with the process, and cannot communicate across processes.

5. Aidl Interaction

Principle: Aidl belongs to the IPC mechanism of Android, it is often used to communicate across processes, and the main implementation principle is based on the underlying binder mechanism.

Pros and Cons Analysis:

Aidl is a common way of interprocess communication in Android and may be used more complex, but with high efficiency and scalability. At the same time, many system services are communicating with the application in this way.

The above is a small series for everyone to talk about the interaction between Android activity and service all content, hope to help everyone, a lot of support cloud Habitat Community ~

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.