If the scenario:
Create two apps Appa and Appb,appa include a service that has a blocking method that generates a random number every 10 seconds, such as the following:
public int Getrandomint () { thread.sleep (10000); return somerandomint;}
APPB calls the method of Service.getrandomint () in Appa and displays the numbers on the interface.
How to resolve:
We all know that. From the level of the general can be divided into processes, tasks, components. Thread. The Android system makes it relatively clear to the developer about the boundaries of the task, the components, and the idea that the process is managed by the process, and that the Android system does not have too many constraints on the thread, which is completely the developer's problem.
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, it is dense operation. This will save you money.
Define the interface yourself:
1) Define an interface yourself. There is an empty method that gets a random number in the interface.
2) The server side inherits from the binder and implements the interface with a class. The empty method that gets the random number is covered.
3) Client side obtains the object of this class through Serviceconnection. This allows you to finally implement real-time interaction by using the method that gets the current download progress.
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. 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. Causes the interface to stutter.
In fact, this wastes the service component. The best way 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 a remote Procedure call. 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 the extension aidl in the Eclipseproject package folder. The interface of the AIDL service is defined in the Aidl file. That is, interface-oriented programming. An empty method containing Getrandomint ().
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)
It can be a lot of other ways to simply implement communication between components instead of calling another component's method:
In the same process:
1) Set the service 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:
Ability to use Boradcast. ContentProvider. Mssenger (Messenger) interactively perform cross-process data transfer.
"Android Personal Understanding (Eight)" method of invoking different components across applications