├── AsyncBinder
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── colors.xml
│ │ │ │ └── styles.xml
│ │ │ ├── mipmap-hdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-anydpi-v26
│ │ │ │ ├── ic_launcher.xml
│ │ │ │ └── ic_launcher_round.xml
│ │ │ ├── layout
│ │ │ │ └── activity_boss.xml
│ │ │ ├── drawable-v24
│ │ │ │ └── ic_launcher_foreground.xml
│ │ │ └── drawable
│ │ │ │ └── ic_launcher_background.xml
│ │ ├── aidl
│ │ │ └── qiwoo
│ │ │ │ └── android
│ │ │ │ └── async
│ │ │ │ └── binder
│ │ │ │ ├── Store.aidl
│ │ │ │ ├── IOrderService.aidl
│ │ │ │ └── IStoreService.aidl
│ │ ├── java
│ │ │ └── qiwoo
│ │ │ │ └── android
│ │ │ │ └── async
│ │ │ │ └── binder
│ │ │ │ ├── OrderService.java
│ │ │ │ ├── StoreService.java
│ │ │ │ ├── Store.java
│ │ │ │ └── BossActivity.java
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── qiwoo
│ │ │ └── android
│ │ │ └── async
│ │ │ └── binder
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── qiwoo
│ │ └── android
│ │ └── async
│ │ └── binder
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── settings.gradle
├── app
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── colors.xml
│ │ │ │ └── styles.xml
│ │ │ ├── mipmap-hdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-anydpi-v26
│ │ │ │ ├── ic_launcher.xml
│ │ │ │ └── ic_launcher_round.xml
│ │ │ ├── layout
│ │ │ │ └── activity_main.xml
│ │ │ ├── drawable-v24
│ │ │ │ └── ic_launcher_foreground.xml
│ │ │ └── drawable
│ │ │ │ └── ic_launcher_background.xml
│ │ ├── aidl
│ │ │ └── qiwoo
│ │ │ │ └── android
│ │ │ │ └── sync
│ │ │ │ └── binder
│ │ │ │ ├── Store.aidl
│ │ │ │ ├── IOrderService.aidl
│ │ │ │ └── IStoreService.aidl
│ │ ├── java
│ │ │ └── qiwoo
│ │ │ │ └── android
│ │ │ │ └── sync
│ │ │ │ └── binder
│ │ │ │ ├── OrderServiceImpl.java
│ │ │ │ ├── StoreServiceImpl.java
│ │ │ │ ├── Store.java
│ │ │ │ ├── BinderCursor.java
│ │ │ │ ├── BinderProvider.java
│ │ │ │ └── ui
│ │ │ │ └── MainActivity.java
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── qiwoo
│ │ │ └── android
│ │ │ └── sync
│ │ │ └── binder
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── qiwoo
│ │ └── android
│ │ └── sync
│ │ └── binder
│ │ └── ExampleInstrumentedTest.java
├── .gitignore
├── proguard-rules.pro
└── build.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── gradle.properties
├── gradlew.bat
├── gradlew
└── README.md
/AsyncBinder/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':AsyncBinder'
2 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | SyncBinder
3 |
4 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yangpeng7/SyncBinder/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea
5 | .DS_Store
6 | /build
7 | /captures
8 | .externalNativeBuild
9 |
--------------------------------------------------------------------------------
/AsyncBinder/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | AsyncBinder
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/aidl/qiwoo/android/sync/binder/Store.aidl:
--------------------------------------------------------------------------------
1 | // Store.aidl
2 | package qiwoo.android.sync.binder;
3 |
4 | parcelable Store;
5 |
--------------------------------------------------------------------------------
/AsyncBinder/src/main/aidl/qiwoo/android/async/binder/Store.aidl:
--------------------------------------------------------------------------------
1 | // Store.aidl.aidl
2 | package qiwoo.android.async.binder;
3 |
4 | parcelable Store;
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | .DS_Store
5 | /build
6 | /captures
7 | .externalNativeBuild
8 | /.idea
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yangpeng7/SyncBinder/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yangpeng7/SyncBinder/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yangpeng7/SyncBinder/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yangpeng7/SyncBinder/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yangpeng7/SyncBinder/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yangpeng7/SyncBinder/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yangpeng7/SyncBinder/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yangpeng7/SyncBinder/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/AsyncBinder/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yangpeng7/SyncBinder/HEAD/AsyncBinder/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/AsyncBinder/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yangpeng7/SyncBinder/HEAD/AsyncBinder/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/AsyncBinder/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yangpeng7/SyncBinder/HEAD/AsyncBinder/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yangpeng7/SyncBinder/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yangpeng7/SyncBinder/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/AsyncBinder/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yangpeng7/SyncBinder/HEAD/AsyncBinder/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/AsyncBinder/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yangpeng7/SyncBinder/HEAD/AsyncBinder/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/AsyncBinder/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yangpeng7/SyncBinder/HEAD/AsyncBinder/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/AsyncBinder/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yangpeng7/SyncBinder/HEAD/AsyncBinder/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/AsyncBinder/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yangpeng7/SyncBinder/HEAD/AsyncBinder/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/AsyncBinder/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yangpeng7/SyncBinder/HEAD/AsyncBinder/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/AsyncBinder/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yangpeng7/SyncBinder/HEAD/AsyncBinder/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/aidl/qiwoo/android/sync/binder/IOrderService.aidl:
--------------------------------------------------------------------------------
1 | // IOrderService.aidl
2 | package qiwoo.android.sync.binder;
3 |
4 | interface IOrderService {
5 |
6 | int getOrderAmount();
7 |
8 | }
9 |
--------------------------------------------------------------------------------
/AsyncBinder/src/main/aidl/qiwoo/android/async/binder/IOrderService.aidl:
--------------------------------------------------------------------------------
1 | // IOrderService.aidl
2 | package qiwoo.android.async.binder;
3 |
4 | interface IOrderService {
5 |
6 | int getOrderAmount();
7 |
8 | }
9 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/aidl/qiwoo/android/sync/binder/IStoreService.aidl:
--------------------------------------------------------------------------------
1 | // IStoreService.aidl
2 | package qiwoo.android.sync.binder;
3 |
4 | import qiwoo.android.sync.binder.Store;
5 |
6 | interface IStoreService {
7 |
8 | List getStores();
9 |
10 | }
11 |
--------------------------------------------------------------------------------
/AsyncBinder/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/AsyncBinder/src/main/aidl/qiwoo/android/async/binder/IStoreService.aidl:
--------------------------------------------------------------------------------
1 | // IStoreService.aidl
2 | package qiwoo.android.async.binder;
3 |
4 | import qiwoo.android.async.binder.Store;
5 |
6 | interface IStoreService {
7 |
8 | List getStores();
9 |
10 | }
11 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed Sep 12 17:53:13 CST 2018
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
7 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/AsyncBinder/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/AsyncBinder/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/java/qiwoo/android/sync/binder/OrderServiceImpl.java:
--------------------------------------------------------------------------------
1 | package qiwoo.android.sync.binder;
2 |
3 | import android.os.RemoteException;
4 |
5 | public class OrderServiceImpl extends IOrderService.Stub {
6 |
7 | @Override
8 | public int getOrderAmount() throws RemoteException {
9 | return 100;
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/AsyncBinder/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/test/java/qiwoo/android/sync/binder/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package qiwoo.android.sync.binder;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/AsyncBinder/src/test/java/qiwoo/android/async/binder/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package qiwoo.android.async.binder;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/app/src/main/java/qiwoo/android/sync/binder/StoreServiceImpl.java:
--------------------------------------------------------------------------------
1 | package qiwoo.android.sync.binder;
2 |
3 | import android.os.RemoteException;
4 |
5 | import java.util.ArrayList;
6 | import java.util.List;
7 |
8 | public class StoreServiceImpl extends IStoreService.Stub {
9 |
10 | private List stores;
11 |
12 | public StoreServiceImpl() {
13 |
14 | Store store1 = new Store(1, "qiwoo", "123", "beijing");
15 | Store store2 = new Store(2, "mobile", "123", "beijing");
16 |
17 | stores = new ArrayList<>();
18 | stores.add(store1);
19 | stores.add(store2);
20 | }
21 |
22 | @Override
23 | public List getStores() throws RemoteException {
24 | return stores;
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
14 |
15 |
20 |
21 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx1536m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 |
--------------------------------------------------------------------------------
/AsyncBinder/src/main/res/layout/activity_boss.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
14 |
15 |
20 |
21 |
--------------------------------------------------------------------------------
/AsyncBinder/src/main/java/qiwoo/android/async/binder/OrderService.java:
--------------------------------------------------------------------------------
1 | package qiwoo.android.async.binder;
2 |
3 | import android.app.Service;
4 | import android.content.Intent;
5 | import android.os.IBinder;
6 | import android.os.RemoteException;
7 | import android.support.annotation.Nullable;
8 |
9 | public class OrderService extends Service {
10 |
11 | @Override
12 | public void onCreate() {
13 | super.onCreate();
14 | }
15 |
16 | IOrderService.Stub mOrderService = new IOrderService.Stub() {
17 | @Override
18 | public int getOrderAmount() throws RemoteException {
19 | return 100;
20 | }
21 | };
22 |
23 | @Nullable
24 | @Override
25 | public IBinder onBind(Intent intent) {
26 | return mOrderService;
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/AsyncBinder/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/qiwoo/android/sync/binder/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package qiwoo.android.sync.binder;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumented test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("qiwoo.android.sync.binder", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/AsyncBinder/src/androidTest/java/qiwoo/android/async/binder/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package qiwoo.android.async.binder;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumented test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("qiwoo.android.async.binder", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 27
5 | defaultConfig {
6 | applicationId "qiwoo.android.sync.binder"
7 | minSdkVersion 16
8 | targetSdkVersion 27
9 | versionCode 1
10 | versionName "1.0"
11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | }
20 |
21 | dependencies {
22 | implementation fileTree(dir: 'libs', include: ['*.jar'])
23 | implementation 'com.android.support:appcompat-v7:27.1.1'
24 | implementation 'com.android.support.constraint:constraint-layout:1.1.3'
25 | testImplementation 'junit:junit:4.12'
26 | androidTestImplementation 'com.android.support.test:runner:1.0.2'
27 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
28 | }
29 |
--------------------------------------------------------------------------------
/AsyncBinder/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
23 |
24 |
27 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/AsyncBinder/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 27
5 |
6 |
7 |
8 | defaultConfig {
9 | applicationId "qiwoo.android.async.binder"
10 | minSdkVersion 16
11 | targetSdkVersion 27
12 | versionCode 1
13 | versionName "1.0"
14 |
15 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
16 |
17 | }
18 |
19 | buildTypes {
20 | release {
21 | minifyEnabled false
22 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
23 | }
24 | }
25 |
26 | }
27 |
28 | dependencies {
29 | implementation fileTree(dir: 'libs', include: ['*.jar'])
30 |
31 | implementation 'com.android.support:appcompat-v7:27.1.1'
32 | implementation 'com.android.support.constraint:constraint-layout:1.1.3'
33 | testImplementation 'junit:junit:4.12'
34 | androidTestImplementation 'com.android.support.test:runner:1.0.2'
35 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
36 | }
37 |
--------------------------------------------------------------------------------
/AsyncBinder/src/main/java/qiwoo/android/async/binder/StoreService.java:
--------------------------------------------------------------------------------
1 | package qiwoo.android.async.binder;
2 |
3 | import android.app.Service;
4 | import android.content.Intent;
5 | import android.os.IBinder;
6 | import android.os.RemoteException;
7 | import android.support.annotation.Nullable;
8 |
9 | import java.util.ArrayList;
10 | import java.util.List;
11 |
12 |
13 | public class StoreService extends Service {
14 |
15 | private List stores;
16 |
17 | @Override
18 | public void onCreate() {
19 | super.onCreate();
20 |
21 | Store store1 = new Store(1, "qiwoo", "123", "beijing");
22 | Store store2 = new Store(2, "mobile", "123", "beijing");
23 |
24 | stores = new ArrayList<>();
25 | stores.add(store1);
26 | stores.add(store2);
27 | }
28 |
29 | IStoreService.Stub mStoreService = new IStoreService.Stub() {
30 |
31 | @Override
32 | public List getStores() throws RemoteException {
33 | return stores;
34 | }
35 | };
36 |
37 | @Nullable
38 | @Override
39 | public IBinder onBind(Intent intent) {
40 | return mStoreService;
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/app/src/main/java/qiwoo/android/sync/binder/Store.java:
--------------------------------------------------------------------------------
1 | package qiwoo.android.sync.binder;
2 |
3 | import android.os.Parcel;
4 | import android.os.Parcelable;
5 |
6 | public class Store implements Parcelable {
7 |
8 | private int id;
9 | private String name;
10 | private String phone;
11 | private String address;
12 |
13 | public int getId() {
14 | return id;
15 | }
16 |
17 | public void setId(int id) {
18 | this.id = id;
19 | }
20 |
21 | public String getName() {
22 | return name;
23 | }
24 |
25 | public void setName(String name) {
26 | this.name = name;
27 | }
28 |
29 | public Store(int id, String name, String phone, String address) {
30 | this.id = id;
31 | this.name = name;
32 | this.phone = phone;
33 | this.address = address;
34 | }
35 |
36 | protected Store(Parcel in) {
37 |
38 | id = in.readInt();
39 | name = in.readString();
40 | phone = in.readString();
41 | address = in.readString();
42 | }
43 |
44 | public static final Creator CREATOR = new Creator() {
45 | @Override
46 | public Store createFromParcel(Parcel in) {
47 | return new Store(in);
48 | }
49 |
50 | @Override
51 | public Store[] newArray(int size) {
52 | return new Store[size];
53 | }
54 | };
55 |
56 | @Override
57 | public int describeContents() {
58 | return 0;
59 | }
60 |
61 | @Override
62 | public void writeToParcel(Parcel dest, int flags) {
63 | dest.writeInt(id);
64 | dest.writeString(name);
65 | dest.writeString(phone);
66 | dest.writeString(address);
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/AsyncBinder/src/main/java/qiwoo/android/async/binder/Store.java:
--------------------------------------------------------------------------------
1 | package qiwoo.android.async.binder;
2 |
3 | import android.os.Parcel;
4 | import android.os.Parcelable;
5 |
6 | public class Store implements Parcelable {
7 |
8 | private int id;
9 | private String name;
10 | private String phone;
11 | private String address;
12 |
13 | public int getId() {
14 | return id;
15 | }
16 |
17 | public void setId(int id) {
18 | this.id = id;
19 | }
20 |
21 | public String getName() {
22 | return name;
23 | }
24 |
25 | public void setName(String name) {
26 | this.name = name;
27 | }
28 |
29 | public Store(int id, String name, String phone, String address) {
30 | this.id = id;
31 | this.name = name;
32 | this.phone = phone;
33 | this.address = address;
34 | }
35 |
36 | protected Store(Parcel in) {
37 |
38 | id = in.readInt();
39 | name = in.readString();
40 | phone = in.readString();
41 | address = in.readString();
42 | }
43 |
44 | public static final Creator CREATOR = new Creator() {
45 | @Override
46 | public Store createFromParcel(Parcel in) {
47 | return new Store(in);
48 | }
49 |
50 | @Override
51 | public Store[] newArray(int size) {
52 | return new Store[size];
53 | }
54 | };
55 |
56 | @Override
57 | public int describeContents() {
58 | return 0;
59 | }
60 |
61 | @Override
62 | public void writeToParcel(Parcel dest, int flags) {
63 | dest.writeInt(id);
64 | dest.writeString(name);
65 | dest.writeString(phone);
66 | dest.writeString(address);
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/app/src/main/java/qiwoo/android/sync/binder/BinderCursor.java:
--------------------------------------------------------------------------------
1 | package qiwoo.android.sync.binder;
2 |
3 | import android.database.MatrixCursor;
4 | import android.os.Bundle;
5 | import android.os.IBinder;
6 | import android.os.Parcel;
7 | import android.os.Parcelable;
8 |
9 | public class BinderCursor extends MatrixCursor {
10 |
11 | static final String KEY_BINDER = "binder";
12 |
13 | Bundle mBinderExtra = new Bundle();
14 |
15 | public static class BinderParcelable implements Parcelable {
16 |
17 | public IBinder mBinder;
18 |
19 | public static final Creator CREATOR = new Creator() {
20 | @Override
21 | public BinderParcelable createFromParcel(Parcel source) {
22 | return new BinderParcelable(source);
23 | }
24 |
25 | @Override
26 | public BinderParcelable[] newArray(int size) {
27 | return new BinderParcelable[size];
28 | }
29 | };
30 |
31 | BinderParcelable(IBinder binder) {
32 | mBinder = binder;
33 | }
34 |
35 | BinderParcelable(Parcel source) {
36 | mBinder = source.readStrongBinder();
37 | }
38 |
39 | @Override
40 | public int describeContents() {
41 | return 0;
42 | }
43 |
44 | @Override
45 | public void writeToParcel(Parcel dest, int flags) {
46 | dest.writeStrongBinder(mBinder);
47 | }
48 | }
49 |
50 | public BinderCursor(String[] columnNames, IBinder binder) {
51 | super(columnNames);
52 |
53 | if (binder != null) {
54 | Parcelable value = new BinderParcelable(binder);
55 | mBinderExtra.putParcelable(KEY_BINDER, value);
56 | }
57 | }
58 |
59 | @Override
60 | public Bundle getExtras() {
61 | return mBinderExtra;
62 | }
63 |
64 | }
65 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/AsyncBinder/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/app/src/main/java/qiwoo/android/sync/binder/BinderProvider.java:
--------------------------------------------------------------------------------
1 | package qiwoo.android.sync.binder;
2 |
3 | import android.content.ContentProvider;
4 | import android.content.ContentValues;
5 | import android.database.Cursor;
6 | import android.net.Uri;
7 | import android.os.IBinder;
8 | import android.support.annotation.NonNull;
9 | import android.support.annotation.Nullable;
10 | import android.util.Log;
11 |
12 | import static qiwoo.android.sync.binder.ui.MainActivity.SERVICE_ORDER;
13 | import static qiwoo.android.sync.binder.ui.MainActivity.SERVICE_STORE;
14 |
15 |
16 | public class BinderProvider extends ContentProvider {
17 |
18 | public static final String TAG = BinderProvider.class.getSimpleName();
19 |
20 | @Override
21 | public boolean onCreate() {
22 | return true;
23 | }
24 |
25 | @Nullable
26 | @Override
27 | public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
28 |
29 | IBinder binder;
30 |
31 | if (selectionArgs[0].equals(SERVICE_ORDER)) {
32 | binder = new OrderServiceImpl();
33 |
34 | Log.d(TAG, "Query OrderServiceImpl");
35 |
36 | } else if (selectionArgs[0].equals(SERVICE_STORE)) {
37 | binder = new StoreServiceImpl();
38 |
39 | Log.d(TAG, "Query StoreServiceImpl");
40 |
41 | } else {
42 | return null;
43 | }
44 |
45 | BinderCursor cursor = new BinderCursor(new String[]{"service"}, binder);
46 |
47 | return cursor;
48 | }
49 |
50 | @Nullable
51 | @Override
52 | public String getType(@NonNull Uri uri) {
53 | return null;
54 | }
55 |
56 | @Nullable
57 | @Override
58 | public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
59 |
60 | return null;
61 | }
62 |
63 | @Override
64 | public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
65 | return 0;
66 | }
67 |
68 | @Override
69 | public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
70 |
71 | return 0;
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/AsyncBinder/src/main/java/qiwoo/android/async/binder/BossActivity.java:
--------------------------------------------------------------------------------
1 | package qiwoo.android.async.binder;
2 |
3 | import android.content.ComponentName;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import android.content.ServiceConnection;
7 | import android.os.Bundle;
8 | import android.os.IBinder;
9 | import android.os.RemoteException;
10 | import android.support.v7.app.AppCompatActivity;
11 | import android.view.View;
12 | import android.widget.Button;
13 | import android.widget.Toast;
14 |
15 | import java.util.List;
16 |
17 | public class BossActivity extends AppCompatActivity {
18 |
19 | private Button mOrderButton;
20 | private Button mStoreButton;
21 |
22 | private IStoreService mStoreService;
23 | private ServiceConnection mStoreServiceConnection = new ServiceConnection() {
24 | @Override
25 | public void onServiceConnected(ComponentName name, IBinder service) {
26 | mStoreService = IStoreService.Stub.asInterface(service);
27 | }
28 |
29 | @Override
30 | public void onServiceDisconnected(ComponentName name) {
31 |
32 | }
33 | };
34 |
35 |
36 | private IOrderService mOrderService;
37 | private ServiceConnection mOrderServiceConnection = new ServiceConnection() {
38 | @Override
39 | public void onServiceConnected(ComponentName name, IBinder service) {
40 | mOrderService = IOrderService.Stub.asInterface(service);
41 | }
42 |
43 | @Override
44 | public void onServiceDisconnected(ComponentName name) {
45 |
46 | }
47 | };
48 |
49 | @Override
50 | protected void onCreate(Bundle savedInstanceState) {
51 | super.onCreate(savedInstanceState);
52 | setContentView(R.layout.activity_boss);
53 |
54 | initViews();
55 |
56 | Intent orderIntent = new Intent();
57 | orderIntent.setClass(this, OrderService.class);
58 | bindService(orderIntent, mOrderServiceConnection, Context.BIND_AUTO_CREATE);
59 |
60 |
61 | Intent storeIntent = new Intent();
62 | storeIntent.setClass(this, StoreService.class);
63 | bindService(storeIntent, mStoreServiceConnection, Context.BIND_AUTO_CREATE);
64 |
65 | }
66 |
67 | private void initViews() {
68 | mOrderButton = findViewById(R.id.order);
69 | mStoreButton = findViewById(R.id.store);
70 |
71 | mOrderButton.setOnClickListener(new View.OnClickListener() {
72 | @Override
73 | public void onClick(View v) {
74 |
75 | try {
76 | int amount = mOrderService.getOrderAmount();
77 | Toast.makeText(BossActivity.this, "恭喜老板营业额是:" + amount + " 亿", Toast.LENGTH_SHORT).show();
78 | } catch (RemoteException e) {
79 | e.printStackTrace();
80 | }
81 | }
82 | });
83 |
84 | mStoreButton.setOnClickListener(new View.OnClickListener() {
85 | @Override
86 | public void onClick(View v) {
87 | if (mStoreService != null) {
88 |
89 | try {
90 | List stores = mStoreService.getStores();
91 | Toast.makeText(BossActivity.this, "报告老板我们现在有:" + stores.size() + " 家超市", Toast.LENGTH_SHORT).show();
92 | } catch (RemoteException e) {
93 | e.printStackTrace();
94 | }
95 | }
96 | }
97 | });
98 | }
99 | }
100 |
--------------------------------------------------------------------------------
/app/src/main/java/qiwoo/android/sync/binder/ui/MainActivity.java:
--------------------------------------------------------------------------------
1 | package qiwoo.android.sync.binder.ui;
2 |
3 | import android.content.ContentResolver;
4 | import android.database.Cursor;
5 | import android.net.Uri;
6 | import android.os.Bundle;
7 | import android.os.IBinder;
8 | import android.os.RemoteException;
9 | import android.support.v7.app.AppCompatActivity;
10 | import android.util.Log;
11 | import android.view.View;
12 | import android.widget.Button;
13 | import android.widget.Toast;
14 |
15 | import java.util.List;
16 |
17 | import qiwoo.android.sync.binder.BinderCursor;
18 | import qiwoo.android.sync.binder.IOrderService;
19 | import qiwoo.android.sync.binder.IStoreService;
20 | import qiwoo.android.sync.binder.R;
21 | import qiwoo.android.sync.binder.Store;
22 |
23 | public class MainActivity extends AppCompatActivity {
24 |
25 | public static final String TAG = MainActivity.class.getName();
26 |
27 | public static final String AUTHORITY = "qiwoo.android.sync.binder.service";
28 | public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/binder");
29 |
30 | public static final String SERVICE_ORDER = "order";
31 | public static final String SERVICE_STORE = "store";
32 |
33 |
34 | private Button mOrderButton;
35 | private Button mStoreButton;
36 |
37 | @Override
38 | protected void onCreate(Bundle savedInstanceState) {
39 | super.onCreate(savedInstanceState);
40 | setContentView(R.layout.activity_main);
41 |
42 | initViews();
43 | }
44 |
45 | private void initViews() {
46 | mOrderButton = findViewById(R.id.order);
47 | mStoreButton = findViewById(R.id.store);
48 |
49 | mOrderButton.setOnClickListener(new View.OnClickListener() {
50 | @Override
51 | public void onClick(View v) {
52 |
53 | final ContentResolver resolver = MainActivity.this.getContentResolver();
54 |
55 | final Cursor cu = resolver.query(CONTENT_URI, null, null, new String[]{SERVICE_ORDER}, null);
56 | if (cu == null) {
57 | return;
58 | }
59 |
60 | IBinder binder = getBinder(cu);
61 | try {
62 | IOrderService orderService = IOrderService.Stub.asInterface(binder);
63 | int amount = orderService.getOrderAmount();
64 |
65 | Toast.makeText(MainActivity.this, "恭喜老板营业额是:" + amount + " 亿", Toast.LENGTH_SHORT).show();
66 |
67 | } catch (RemoteException e) {
68 | e.printStackTrace();
69 | }
70 |
71 | cu.close();
72 | }
73 | });
74 |
75 | mStoreButton.setOnClickListener(new View.OnClickListener() {
76 | @Override
77 | public void onClick(View v) {
78 | final ContentResolver resolver = MainActivity.this.getContentResolver();
79 |
80 | final Cursor cu = resolver.query(CONTENT_URI, null, null, new String[]{SERVICE_STORE}, null);
81 | if (cu == null) {
82 | return;
83 | }
84 |
85 | IBinder binder = getBinder(cu);
86 | try {
87 | IStoreService storeService = IStoreService.Stub.asInterface(binder);
88 | List stores = storeService.getStores();
89 |
90 | Toast.makeText(MainActivity.this, "报告老板我们现在有:" + stores.size() + " 家超市", Toast.LENGTH_SHORT).show();
91 |
92 | } catch (RemoteException e) {
93 | e.printStackTrace();
94 | }
95 |
96 | cu.close();
97 | }
98 | });
99 | }
100 |
101 |
102 | public static final IBinder getBinder(Cursor cursor) {
103 | Bundle extras = cursor.getExtras();
104 | extras.setClassLoader(BinderCursor.BinderParcelable.class.getClassLoader());
105 | BinderCursor.BinderParcelable w = extras.getParcelable("binder");
106 | return w.mBinder;
107 | }
108 | }
109 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/AsyncBinder/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | [博客:https://jiudian.link/](https://jiudian.link/2018/09/14/%E5%88%A9%E7%94%A8ContentProvider%E5%AE%9E%E7%8E%B0%E5%90%8C%E6%AD%A5Binder/)
2 |
3 | ##### 在Android中跨进程通信的方式有好多种,比如
4 |
5 | * Intent
6 | * Messenger
7 | * AIDL(Android 接口定义语言)
8 | * ContentProvider
9 | * Socket
10 |
11 | ##### 以AIDL为例,在使用AIDL实现安卓跨进程通信的时候,通常分为3步:
12 |
13 | 1. 定义AIDL接口文件,在`Service`的`onBind`方法中返回`binder`给客户端
14 |
15 | 2. 客户端与服务端绑定,在回调函数`onServiceConnected`中获取`binder`
16 |
17 | 3. 通过`Stub`的`asInterface`方法转换为我们定义的接口,然后调用服务端逻辑。
18 |
19 | 这是一种典型的CS(客户端-服务端)架构。下面我们就用AIDL来实现跨进程通信,首先我们来定义一个问题:
20 |
21 | > 假如小王是一家连锁超市的老板,他最关心的是自己的超市目前的规模以及自己超市的营业额的情况。他是需要服务的一端,所以把小王定义为客户端。针对老板的需求,我们需要提供两个服务,一是查询连锁超市的数量而是查询超市的营业额。
22 |
23 | 既然需求有了,现在我们就来实现它:
24 |
25 | 客户端就定义一个`BossActivity`用于显示连锁超市目前的规模以及营业额。
26 | 服务端为了解耦就定义两个`Service`,`OrderService`(查询营业额)和`StoreService`(查询超市规模)
27 |
28 | 接下来按照上面的三步走,我们依次来实现一下
29 |
30 | #### 1. 定义AIDL
31 | - 定义`IOrderService.aidl` 包括查询营业额的服务
32 |
33 | ```
34 | package qiwoo.android.sync.binder;
35 |
36 | interface IOrderService {
37 |
38 | // 获取营业额
39 | int getOrderAmount();
40 |
41 | }
42 |
43 | ```
44 |
45 | - 定义`IStoreService.aidl` 包括查询超市规模的的服务,其实就是store的数量
46 |
47 | ```
48 | package qiwoo.android.sync.binder;
49 |
50 | import qiwoo.android.sync.binder.Store;
51 |
52 | interface IStoreService {
53 |
54 | // 获取超市的列表
55 | List getStores();
56 |
57 | }
58 |
59 | ```
60 |
61 | #### 2. 在Service中实现接口并作为binder返回
62 |
63 | - `OrderService` 具体实现如下
64 |
65 | ```
66 | public class OrderService extends Service {
67 |
68 | @Override
69 | public void onCreate() {
70 | super.onCreate();
71 | }
72 |
73 | IOrderService.Stub mOrderService = new IOrderService.Stub() {
74 | @Override
75 | public int getOrderAmount() throws RemoteException {
76 | // 方便演示这里就不涉及过多逻辑,简单返回数据
77 | return 100;
78 | }
79 | };
80 |
81 | @Nullable
82 | @Override
83 | public IBinder onBind(Intent intent) {
84 | //返回 OrderService binder
85 | return mOrderService;
86 | }
87 | }
88 |
89 | ```
90 |
91 |
92 | - `StoreService` 具体实现如下
93 |
94 | ```
95 | public class StoreService extends Service {
96 |
97 | private List stores;
98 |
99 | @Override
100 | public void onCreate() {
101 | super.onCreate();
102 |
103 | // 方便演示这里就不涉及过多逻辑,简单创建数据
104 | Store store1 = new Store(1, "qiwoo", "123", "beijing");
105 | Store store2 = new Store(2, "mobile", "123", "beijing");
106 |
107 | stores = new ArrayList<>();
108 | stores.add(store1);
109 | stores.add(store2);
110 | }
111 |
112 | IStoreService.Stub mStoreService = new IStoreService.Stub() {
113 |
114 | @Override
115 | public List getStores() throws RemoteException {
116 | return stores;
117 | }
118 | };
119 |
120 | @Nullable
121 | @Override
122 | public IBinder onBind(Intent intent) {
123 | // 返回 StoreService binder
124 | return mStoreService;
125 | }
126 | }
127 |
128 |
129 | ```
130 |
131 | #### 3. 在`BossActivity`中 `bindService`
132 |
133 | - 绑定服务
134 |
135 | ```
136 | Intent orderIntent = new Intent();
137 | orderIntent.setClass(this, OrderService.class);
138 | bindService(orderIntent, mOrderServiceConnection, Context.BIND_AUTO_CREATE);
139 |
140 | Intent storeIntent = new Intent();
141 | storeIntent.setClass(this, StoreService.class);
142 | bindService(storeIntent, mStoreServiceConnection, Context.BIND_AUTO_CREATE);
143 |
144 | ```
145 | - 传入的ServiceConnection
146 |
147 | ```
148 | private ServiceConnection mOrderServiceConnection = new ServiceConnection() {
149 | @Override
150 | public void onServiceConnected(ComponentName name, IBinder service) {
151 | // 在这里,绑定成功之后,我们就拿到了binder
152 | mOrderService = IOrderService.Stub.asInterface(service);
153 | }
154 |
155 | @Override
156 | public void onServiceDisconnected(ComponentName name) {
157 |
158 | }
159 | };
160 |
161 |
162 | ```
163 |
164 | - 调用远程服务
165 |
166 | ```
167 | try {
168 | // 在 onServiceConnected 中拿到的binder
169 | int amount = mOrderService.getOrderAmount();
170 | Toast.makeText(BossActivity.this, "恭喜老板营业额是:" + amount + " 亿", Toast.LENGTH_SHORT).show();
171 | } catch (RemoteException e) {
172 | e.printStackTrace();
173 | }
174 |
175 | ```
176 |
177 | 到目前为止,一切都很顺利。我们通过AIDL实现了跨进程通信,来测试一下结果和我们预期的也一样。
178 |
179 |
180 |
181 |
182 |
183 |
184 |
185 |
189 | 非常棒,老板很有钱,晚上又可以给我们加鸡腿了。但是有没有发现一个问题:在`BossActivity`中想要调用另一个进程的服务,必须要等 `bindService`中传入的`ServiceConnection`拿到`onServiceConnected`的回调才能使用,也就是我们异步的获取了`binder`。有的时候我们并不想这样做,有没有一个办法可以直接通过一个`get`方法就拿到`binder`呢?
190 |
191 | 答案当然是肯定的啦,现在我们再回到文章开头看看实现跨进程通信中常见的几种方式,有一个`ContentProvider`接下来它就是我们的主角了,对`ContentProvider`不熟的同学可以去查一下它的用法。我们就通过它来实现在客户端同步获取`binder`,怎么去做呢,同样三步走。
192 |
193 | - 定义AIDL接口文件和实现类
194 |
195 | - 定义一个`ContentProvider`根据查询参数的不同返回具体的服务`binder`
196 |
197 | - 查询`ContentProvider`获得`Cursor`然后通过`Stub`的`asInterface`方法转换为我们定义的接口,然后调用服务端逻辑。
198 |
199 | #### 1. 定义AIDL和实现类
200 |
201 | AIDL和上面完全一样,不再重复
202 |
203 | - `OrderServiceImpl`实现
204 |
205 | ```
206 | public class OrderServiceImpl extends IOrderService.Stub {
207 |
208 | @Override
209 | public int getOrderAmount() throws RemoteException {
210 | return 100;
211 | }
212 | }
213 |
214 | ```
215 |
216 | - `StoreServiceImpl`实现
217 |
218 | ```
219 | public class StoreServiceImpl extends IStoreService.Stub {
220 |
221 | private List stores;
222 |
223 | public StoreServiceImpl() {
224 |
225 | Store store1 = new Store(1, "qiwoo", "123", "beijing");
226 | Store store2 = new Store(2, "mobile", "123", "beijing");
227 |
228 | stores = new ArrayList<>();
229 | stores.add(store1);
230 | stores.add(store2);
231 | }
232 |
233 | @Override
234 | public List getStores() throws RemoteException {
235 | return stores;
236 | }
237 | }
238 |
239 | ```
240 |
241 | #### 2. 定义`BinderProvider`主要代码如下:
242 |
243 | ```
244 | public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
245 |
246 | IBinder binder;
247 |
248 | if (selectionArgs[0].equals(SERVICE_ORDER)) {
249 | binder = new OrderServiceImpl();
250 |
251 | Log.d(TAG, "Query OrderServiceImpl");
252 |
253 | } else if (selectionArgs[0].equals(SERVICE_STORE)) {
254 | binder = new StoreServiceImpl();
255 |
256 | Log.d(TAG, "Query StoreServiceImpl");
257 |
258 | } else {
259 | return null;
260 | }
261 |
262 | BinderCursor cursor = new BinderCursor(new String[]{"service"}, binder);
263 |
264 | return cursor;
265 | }
266 |
267 | ```
268 |
269 | `ContentProvider`的`query`方法返回的是一个`Cursor`,现在的场景不像查询数据库一样可以通过`SQLiteDatabase`的`query`方法直接返回一个`Cursor`,而`Cursor`又是一个接口,没有办法直接实例化。所以我们需要找一个可以实例化一个`Cursor`,这里用到了`MatrixCursor`。有了`Cursor`之后就可以把根据查询参数的不同我们返回了不同的`binder`放到`Cursor`中返回。下面我们来看一下`BinderCursor`
270 |
271 | ```
272 | public class BinderCursor extends MatrixCursor {
273 |
274 | static final String KEY_BINDER = "binder";
275 |
276 | Bundle mBinderExtra = new Bundle();
277 |
278 | public static class BinderParcelable implements Parcelable {
279 |
280 | public IBinder mBinder;
281 |
282 | public static final Creator CREATOR = new Creator() {
283 | @Override
284 | public BinderParcelable createFromParcel(Parcel source) {
285 | return new BinderParcelable(source);
286 | }
287 |
288 | @Override
289 | public BinderParcelable[] newArray(int size) {
290 | return new BinderParcelable[size];
291 | }
292 | };
293 |
294 | BinderParcelable(IBinder binder) {
295 | mBinder = binder;
296 | }
297 |
298 | BinderParcelable(Parcel source) {
299 | mBinder = source.readStrongBinder();
300 | }
301 |
302 | @Override
303 | public int describeContents() {
304 | return 0;
305 | }
306 |
307 | @Override
308 | public void writeToParcel(Parcel dest, int flags) {
309 | dest.writeStrongBinder(mBinder);
310 | }
311 | }
312 |
313 | public BinderCursor(String[] columnNames, IBinder binder) {
314 | super(columnNames);
315 |
316 | if (binder != null) {
317 | Parcelable value = new BinderParcelable(binder);
318 | mBinderExtra.putParcelable(KEY_BINDER, value);
319 | }
320 | }
321 |
322 | @Override
323 | public Bundle getExtras() {
324 | return mBinderExtra;
325 | }
326 |
327 | }
328 |
329 | ```
330 |
331 | 可以看到它继承自`MatrixCursor`,然后通过`Bundle`包装了`binder`,这样就可以 `new` 一个 `MatrixCursor `的对象返回了。
332 |
333 |
334 | #### 3. 查询`ContentProvider`获得`cursor`调用服务端逻辑。
335 |
336 | ```
337 | final ContentResolver resolver = MainActivity.this.getContentResolver();
338 |
339 | final Cursor cu = resolver.query(CONTENT_URI, null, null, new String[]{SERVICE_ORDER}, null);
340 | if (cu == null) {
341 | return;
342 | }
343 |
344 | IBinder binder = getBinder(cu);
345 | try {
346 | IOrderService orderService = IOrderService.Stub.asInterface(binder);
347 | int amount = orderService.getOrderAmount();
348 |
349 | Toast.makeText(MainActivity.this, "恭喜老板营业额是:" + amount + " 亿", Toast.LENGTH_SHORT).show();
350 |
351 | } catch (RemoteException e) {
352 | e.printStackTrace();
353 | }
354 |
355 | cu.close();
356 |
357 |
358 | ```
359 | 这里,我们为了获得营业额和超市规模的数据传入的查询参数是`SERVICE_ORDER `和`SERVICE_STORE`,假如有很多个服务就可以把这部分代码再进行封装,写一个管理类,根据不同的参数返回不同的service。
360 |
361 |
362 | 这样我们获取`binder`就是同步的了,不需要再等待回调,`query`出来直接使用。打印的结果和上面是一样的,不再展示。
363 |
--------------------------------------------------------------------------------