The original text is also published in my blog
I can see more in the entrance.
Requirements background
Recently received a need to interact with other apps, such as the download app and desktop app linkage, desktop app can directly display the download app download task progress and status.
Find Solutions
As you know from the requirements, the main problem is how to solve the cross-process communication top.
Aidl
AIDL, the acronym for Android Interface Definition language, is a descriptive language designed for cross-process communication interfaces in Android. The pros and cons are obvious and the pros are stable, fast, and Android is designed for cross-process communication. The disadvantage is that the aidl is a communication contract, both parties involved in the communication need to add the Aidl file to their code, and then create the Service to achieve access and access.
ContentProvider
As one of the four basic components of Android, ContentProvider's role is simply to provide cross-process access to the nature of the content. But in API one (Android 3.0), ContentProvider added a new method that can be used to make cross-process method calls, as defined in the ContentProvider method:
Bundle call(String method, String arg, Bundle extras)
In terms of ease of use, this is not aidl so troublesome, and more extensibility, and no broadcast too dependent on the system, API 11 should be the main drawback, and other shortcomings temporarily did not find, welcome to add.
Broadcast
Broadcast is the simplest: the advantage is that the task of distributing messages to the Android system, the disadvantage is because all the system, many places are not controlled. Disadvantages:
- Although a broadcast can send a directional message by specifying a package name, it cannot verify the signature of the message to the APP.
- After the system restarts, messages inside the system's broadcast queue are lost.
Realize
In order to brief, the main talk about ContentProvider bar.
ContentProvider
The first is the ContentProvider code implementation of the Downloader APP
PackageCn.hiroz.downloader.realname;ImportAndroid.content.ContentProvider;ImportAndroid.content.ContentValues;ImportAndroid.database.Cursor;ImportAndroid.net.Uri;ImportAndroid.os.Binder;ImportAndroid.os.Bundle;ImportAndroid.util.Log; Public class downloadercontentprovider extends contentprovider { @Override Public Boolean onCreate() {return false; }@Override PublicCursorQuery(Uri Uri, string[] strings, string s, string[] strings2, string s2) {return NULL; }@Override PublicStringGetType(URI Uri) {return NULL; }@Override PublicUriInsert(Uri Uri, contentvalues contentvalues) {return NULL; }@Override Public int Delete(Uri Uri, String s, string[] strings) {return 0; }@Override Public int Update(Uri Uri, Contentvalues contentvalues, String S, string[] strings) {return 0; }@Override PublicBundlePager(String method, string arg, Bundle extras) {if("DOWNLOAD". Equals (method)) {//When calling me to downloadLOG.E ("Downloader","Download:"+ arg);//Call the Desktop App's method to update the statusUpdateStatus ("Download"); }Else("PAUSE". Equals (method)) {//When calling me to pauseLOG.E ("Downloader","Pause:"+ arg);//Call the Desktop App's method to update the statusUpdateStatus ("Pause"); }return NULL; }//We want to invoke the URI of the other's ContentProvider Private FinalUri Launchercontentprovider_uri = Uri.parse ("Content://cn.hiroz.launcher.launchercontentprovider");}Private void UpdateStatus(String status) {GetContext (). Getcontentresolver (). Call (Launchercontentprovider_uri,"Update_status", Status,NewBundle ()); }
You also need to add contentprovider definitions in the androidmanifest.xml of the downloader App:
<provider android:name="cn.hiroz.downloader.realname.DownloaderContentProvider" android:authorities="cn.hiroz.downloader.DownloaderContentProvider" android:exported="true"/>
I specifically added the authorities setting so that the URI of the ContentProvider that was accessed during the interaction would look different and would not expose my real contentprovider class
Then the ContentProvider code implementation of the desktop APP
PackageCn.hiroz.launcher.realname;ImportAndroid.content.ContentProvider;ImportAndroid.content.ContentValues;ImportAndroid.database.Cursor;ImportAndroid.net.Uri;ImportAndroid.os.Binder;ImportAndroid.os.Bundle;ImportAndroid.util.Log; Public class launchercontentprovider extends contentprovider { @Override Public Boolean onCreate() {return false; }@Override PublicCursorQuery(Uri Uri, string[] strings, string s, string[] strings2, string s2) {return NULL; }@Override PublicStringGetType(URI Uri) {return NULL; }@Override PublicUriInsert(Uri Uri, contentvalues contentvalues) {return NULL; }@Override Public int Delete(Uri Uri, String s, string[] strings) {return 0; }@Override Public int Update(Uri Uri, Contentvalues contentvalues, String S, string[] strings) {return 0; }@Override PublicBundlePager(String method, string arg, Bundle extras) {//When the "update status" is called if("Update_status". Equals (method)) {LOG.E ("Launcher","Update Status:"+ arg); }return NULL; }//We want to invoke the URI of the other's ContentProvider Private FinalUri Downloadercontentprovider_uri = Uri.parse ("Content://cn.hiroz.downloader.downloadercontentprovider");} Public void Download(String Arg) {GetContext (). Getcontentresolver (). Call (Downloadercontentprovider_uri,"DOWNLOAD", Status,NewBundle ()); } Public void Pause(String Arg) {GetContext (). Getcontentresolver (). Call (Downloadercontentprovider_uri,"PAUSE", Status,NewBundle ()); }}
You also need to add ContentProvider definitions in the desktop APP's androidmanifest.xml:
<provider android:name="cn.hiroz.launcher.realname.LauncherContentProvider" android:authorities="cn.hiroz.launcher.LauncherContentProvider" android:exported="true"/>
Then in the Desktop app, you can use the Launchercontentprovider download
method and pause
method to invoke the function of the downloader App (these two methods are not suitable for writing here, but I just want to save space to put together). When the method is called in the Downloader app, the update status of the desktop app is called.
Here is just a demonstration of an interactive process, there are more questions to welcome you to discuss learning ~ ~
Extended
Cross-process method calls using ContentProvider in Android