├── AIDLClient ├── .gitignore ├── .idea │ ├── gradle.xml │ ├── misc.xml │ ├── modules.xml │ └── runConfigurations.xml ├── app │ ├── .gitignore │ ├── build.gradle │ ├── proguard-rules.pro │ └── src │ │ ├── androidTest │ │ └── java │ │ │ └── com │ │ │ └── wuzy │ │ │ └── aidlclient │ │ │ └── ExampleInstrumentedTest.java │ │ ├── main │ │ ├── AndroidManifest.xml │ │ ├── aidl │ │ │ └── com │ │ │ │ └── wuzy │ │ │ │ └── aidlserver │ │ │ │ └── IMyAidlInterface.aidl │ │ ├── java │ │ │ └── com │ │ │ │ └── wuzy │ │ │ │ └── aidlclient │ │ │ │ ├── LocalService.java │ │ │ │ ├── MainActivity.java │ │ │ │ └── TransferActivity.java │ │ └── res │ │ │ ├── drawable-v24 │ │ │ └── ic_launcher_foreground.xml │ │ │ ├── drawable │ │ │ └── ic_launcher_background.xml │ │ │ ├── layout │ │ │ ├── activity_main.xml │ │ │ └── activity_transfer.xml │ │ │ ├── mipmap-anydpi-v26 │ │ │ ├── ic_launcher.xml │ │ │ └── ic_launcher_round.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 │ │ │ └── values │ │ │ ├── colors.xml │ │ │ ├── strings.xml │ │ │ └── styles.xml │ │ └── test │ │ └── java │ │ └── com │ │ └── wuzy │ │ └── aidlclient │ │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle ├── AIDLServer ├── .gitignore ├── .idea │ ├── gradle.xml │ ├── misc.xml │ ├── modules.xml │ ├── runConfigurations.xml │ └── vcs.xml ├── app │ ├── .gitignore │ ├── build.gradle │ ├── proguard-rules.pro │ └── src │ │ ├── androidTest │ │ └── java │ │ │ └── com │ │ │ └── wuzy │ │ │ └── aidlserver │ │ │ └── ExampleInstrumentedTest.java │ │ ├── main │ │ ├── AndroidManifest.xml │ │ ├── aidl │ │ │ └── com │ │ │ │ └── wuzy │ │ │ │ └── aidlserver │ │ │ │ └── IMyAidlInterface.aidl │ │ ├── java │ │ │ └── com │ │ │ │ └── wuzy │ │ │ │ └── aidlserver │ │ │ │ ├── MainActivity.java │ │ │ │ ├── RemoteService.java │ │ │ │ └── TransferActivity.java │ │ └── res │ │ │ ├── drawable-v24 │ │ │ └── ic_launcher_foreground.xml │ │ │ ├── drawable │ │ │ └── ic_launcher_background.xml │ │ │ ├── layout │ │ │ ├── activity_main.xml │ │ │ └── activity_transfer.xml │ │ │ ├── mipmap-anydpi-v26 │ │ │ ├── ic_launcher.xml │ │ │ └── ic_launcher_round.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 │ │ │ └── values │ │ │ ├── colors.xml │ │ │ ├── strings.xml │ │ │ └── styles.xml │ │ └── test │ │ └── java │ │ └── com │ │ └── wuzy │ │ └── aidlserver │ │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle └── README.md /AIDLClient/.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /AIDLClient/.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /AIDLClient/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 16 | 26 | 27 | 28 | 29 | 30 | 31 | 33 | -------------------------------------------------------------------------------- /AIDLClient/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /AIDLClient/.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /AIDLClient/app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /AIDLClient/app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 27 5 | defaultConfig { 6 | applicationId "com.wuzy.aidlclient" 7 | minSdkVersion 15 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 | -------------------------------------------------------------------------------- /AIDLClient/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 | -------------------------------------------------------------------------------- /AIDLClient/app/src/androidTest/java/com/wuzy/aidlclient/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.wuzy.aidlclient; 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() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.wuzy.aidlclient", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /AIDLClient/app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 23 | 24 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /AIDLClient/app/src/main/aidl/com/wuzy/aidlserver/IMyAidlInterface.aidl: -------------------------------------------------------------------------------- 1 | // IMyAidlInterface.aidl 2 | package com.wuzy.aidlserver; 3 | 4 | // Declare any non-default types here with import statements 5 | 6 | interface IMyAidlInterface { 7 | 8 | void bindSuccess(); 9 | 10 | void unbind(); 11 | 12 | } 13 | -------------------------------------------------------------------------------- /AIDLClient/app/src/main/java/com/wuzy/aidlclient/LocalService.java: -------------------------------------------------------------------------------- 1 | package com.wuzy.aidlclient; 2 | 3 | import android.app.Service; 4 | import android.content.ComponentName; 5 | import android.content.Context; 6 | import android.content.Intent; 7 | import android.content.ServiceConnection; 8 | import android.os.Handler; 9 | import android.os.IBinder; 10 | import android.os.RemoteException; 11 | import android.util.Log; 12 | 13 | import com.wuzy.aidlserver.IMyAidlInterface; 14 | 15 | public class LocalService extends Service { 16 | 17 | private static final String TAG = "LocalService"; 18 | 19 | private IMyAidlInterface iMyAidlInterface; 20 | 21 | private boolean mIsBound; 22 | 23 | @Override 24 | public void onCreate() { 25 | super.onCreate(); 26 | Log.e(TAG, "onCreate: 创建 LocalService"); 27 | 28 | // // 模拟 5 秒后解除绑定 29 | // new Handler().postDelayed(new Runnable() { 30 | // @Override 31 | // public void run() { 32 | // unbindRemoteService(); 33 | // } 34 | // }, 5000); 35 | 36 | bindRemoteService(); 37 | 38 | } 39 | 40 | @Override 41 | public int onStartCommand(Intent intent, int flags, int startId) { 42 | Log.e(TAG, "onStartCommand: " ); 43 | return START_STICKY; 44 | } 45 | 46 | @Override 47 | public IBinder onBind(Intent intent) { 48 | Log.e(TAG, "onBind: 绑定 LocalService"); 49 | return stub; 50 | } 51 | 52 | @Override 53 | public boolean onUnbind(Intent intent) { 54 | Log.e(TAG, "onUnbind: 解绑 LocalService"); 55 | return super.onUnbind(intent); 56 | } 57 | 58 | @Override 59 | public void onDestroy() { 60 | Log.e(TAG, "onDestroy: 销毁 LocalService"); 61 | super.onDestroy(); 62 | } 63 | 64 | private IMyAidlInterface.Stub stub = new IMyAidlInterface.Stub() { 65 | @Override 66 | public void bindSuccess() throws RemoteException { 67 | Log.e(TAG, "bindSuccess: RemoteService 绑定 LocalService 成功"); 68 | } 69 | 70 | @Override 71 | public void unbind() throws RemoteException { 72 | getApplicationContext().unbindService(connection); 73 | } 74 | }; 75 | 76 | private ServiceConnection connection = new ServiceConnection() { 77 | @Override 78 | public void onServiceConnected(ComponentName name, IBinder service) { 79 | Log.e(TAG, "onServiceConnected: RemoteService 链接成功"); 80 | mIsBound = true; 81 | iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service); 82 | try { 83 | iMyAidlInterface.bindSuccess(); 84 | } catch (RemoteException e) { 85 | e.printStackTrace(); 86 | } 87 | } 88 | 89 | @Override 90 | public void onServiceDisconnected(ComponentName name) { 91 | Log.e(TAG, "onServiceDisconnected: RemoteService 断开连接,重新启动"); 92 | mIsBound = false; 93 | createTransferActivity(); 94 | } 95 | }; 96 | 97 | private void createTransferActivity() { 98 | Intent intent = new Intent(this, TransferActivity.class); 99 | intent.setAction(TransferActivity.ACTION_FROM_SELF); 100 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 101 | startActivity(intent); 102 | } 103 | 104 | private void bindRemoteService() { 105 | Intent intent = new Intent(); 106 | intent.setComponent(new ComponentName("com.wuzy.aidlserver", "com.wuzy.aidlserver.RemoteService")); 107 | if (!getApplicationContext().bindService(intent, connection, Context.BIND_AUTO_CREATE)) { 108 | Log.e(TAG, "bindRemoteService: 绑定 RemoteService 失败"); 109 | stopSelf(); 110 | } 111 | } 112 | 113 | /** 114 | * 解除绑定 RemoteService 115 | */ 116 | private void unbindRemoteService() { 117 | if (mIsBound) { 118 | try { 119 | // 先让 RemoteService 解除绑定 LocalService 120 | iMyAidlInterface.unbind(); 121 | } catch (RemoteException e) { 122 | e.printStackTrace(); 123 | } 124 | getApplicationContext().unbindService(connection); // 解除 LocalService 与 RemoteService 125 | stopSelf(); 126 | } 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /AIDLClient/app/src/main/java/com/wuzy/aidlclient/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.wuzy.aidlclient; 2 | 3 | import android.content.Intent; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.os.Bundle; 6 | import android.view.View; 7 | 8 | public class MainActivity extends AppCompatActivity { 9 | 10 | @Override 11 | protected void onCreate(Bundle savedInstanceState) { 12 | super.onCreate(savedInstanceState); 13 | setContentView(R.layout.activity_main); 14 | 15 | findViewById(R.id.btn_start_local_service).setOnClickListener(new View.OnClickListener() { 16 | @Override 17 | public void onClick(View v) { 18 | Intent intent = new Intent(MainActivity.this,LocalService.class); 19 | startService(intent); 20 | } 21 | }); 22 | 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /AIDLClient/app/src/main/java/com/wuzy/aidlclient/TransferActivity.java: -------------------------------------------------------------------------------- 1 | package com.wuzy.aidlclient; 2 | 3 | import android.app.ActivityManager; 4 | import android.content.ComponentName; 5 | import android.content.Context; 6 | import android.content.Intent; 7 | import android.support.v7.app.AppCompatActivity; 8 | import android.os.Bundle; 9 | import android.util.Log; 10 | import android.view.Gravity; 11 | import android.view.Window; 12 | import android.view.WindowManager; 13 | 14 | import java.util.ArrayList; 15 | 16 | public class TransferActivity extends AppCompatActivity { 17 | 18 | public static final String ACTION_FROM_SELF = "com.wuzy.aidlclient.TransferActivity.FROM_SELF"; 19 | public static final String ACTION_FROM_OTHER = "com.wuzy.aidlclient.TransferActivity.FROM_OTHER"; 20 | 21 | private static final String TAG = "TransferActivity"; 22 | 23 | @Override 24 | protected void onCreate(Bundle savedInstanceState) { 25 | super.onCreate(savedInstanceState); 26 | Log.e(TAG, "onCreate: 创建中转 Activity"); 27 | 28 | Window window = getWindow(); 29 | window.setGravity(Gravity.START | Gravity.TOP); 30 | WindowManager.LayoutParams attributes = window.getAttributes(); 31 | attributes.width = attributes.height = 1; 32 | attributes.x = attributes.y = 0; 33 | 34 | if (getIntent() != null) { 35 | Intent intent; 36 | if (ACTION_FROM_OTHER.equals(getIntent().getAction())) { 37 | intent = new Intent(this, LocalService.class); 38 | startService(intent); 39 | } else if (ACTION_FROM_SELF.equals(getIntent().getAction())) { 40 | intent = new Intent("com.wuzy.aidlserver.TransferActivity.FROM_OTHER"); 41 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 42 | intent.setComponent(new ComponentName("com.wuzy.aidlserver", "com.wuzy.aidlserver.TransferActivity")); 43 | startActivity(intent); 44 | } 45 | } 46 | finish(); 47 | } 48 | 49 | @Override 50 | protected void onDestroy() { 51 | super.onDestroy(); 52 | Log.e(TAG, "onDestroy: 销毁中转 Activity"); 53 | } 54 | 55 | //判断Service是否在运行 56 | 57 | private boolean isServiceRunning(Context context, String serviceName) throws ClassNotFoundException { 58 | 59 | if (("").equals(serviceName) || serviceName == null) { 60 | return false; 61 | } 62 | ActivityManager myManager = (ActivityManager) context 63 | .getSystemService(Context.ACTIVITY_SERVICE); 64 | ArrayList runningService = (ArrayList) myManager 65 | .getRunningServices(Integer.MAX_VALUE); 66 | for (int i = 0; i < runningService.size(); i++) { 67 | if (runningService.get(i).service.getClassName().toString() 68 | .equals(serviceName)) { 69 | return true; 70 | } 71 | } 72 | return false; 73 | 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /AIDLClient/app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /AIDLClient/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 | -------------------------------------------------------------------------------- /AIDLClient/app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 |