"Android Personal Understanding (Eight)" method of invoking different components across applications

Source: Internet
Author: User

Hypothetical scenario:

Create two apps Appa and Appb,appa contain a service that has a blocking method that produces a random number every 10 seconds, as follows:
public int Getrandomint () {    thread.sleep (10000);    return somerandomint;}

APPB calls the method of Service.getrandomint () in Appa and displays the numbers on the interface.

Solution:

As we all know, from the level of the general can be divided into processes, tasks, components, threads, Android system so that developers for the task, the component boundaries are relatively clear, and for the process has the process of hosting, and for the thread is completely the developer's own problem, the Android system does not do too much constraints. Because the topic is relatively simple, the component has only one APPB activity and one Appa service, so our hierarchical relationship is:

Process thread Component

1. If two components are in the same process:

By setting <process> properties, the relevant application is plugged into a process so that they can die and have a common memory area. Because the problem requires communication every 10 seconds, is dense operation, this can save money.

Custom interfaces:

1) Customize an interface that has an empty method that gets a random number.

2) server end uses a class to inherit from binder and implement the interface, and overwrite the empty method that obtains the random number.

3) The client side obtains the object of this class through serviceconnection, thus can use the method that obtains the current download progress, finally realizes the real-time interaction.

4) The server-side overrides the Onbind () method, returning the Binder object.

5) Override onserviceconnected in activity to get service object.

6) Open a background thread and call Getrandomint () with the service object.

7) Update the view by returning the data to the main thread via handler.

(Another reason to open a background thread is that the method in Getrandomint () sleep (10000) causes the main thread to hibernate, causing the interface to stutter. In fact, the best way to make the service component waste is to start the service and then communicate.

2. If two components are not in the same process:

Settings <process> attributes, divided into different processes

RPC is the remote Procedure call remoting process, while Android's interprocess communication is implemented through the AIDL service. The binder-based IPC is part of the RPC.

1) Create a file with an extension of aidl in the Eclipse Project's package directory. The interface for the AIDL service is defined in the Aidl file. That is, interface-oriented programming, as above contains Getrandomint () empty method.

2) Set up a service class (subclass of Services). Implements the Getrandomint () generated by the Aidl file.

3) Configure the Aidl service in the Androidmanifest.xml file. That is declared under the Intent-filter tag in the service.

4) Open a background thread in APPB activity, thread range create serviceconnection object, get aidl service object, call Getrandomint () method.

5) Update the view by returning the data to the main thread via handler.

(similar to a single process)

If you simply implement communication between components, rather than calling a method of another component, there are more ways to do it:

In the same process:

1) Set the service's android:exported= "true" in the Appa manifest file.

2) APPB activity sets the listener event and invokes the service that initiates Appa by sending intent through StartService ().

Data is then passed between threads, such as handler, Sharedpreferences, and so on.

And in a different process:

You can use Boradcast,contentprovider,mssenger (Messenger) interactively to pass data across processes.


"Android Personal Understanding (Eight)" method of invoking different components across applications

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.