一、項目介紹
【知識准備】
①Android Interface definition language(aidl,android接口定義語言),其目的實現跨進程的調用。進程是程序在os中執行的載體,一個程序對應一個進程,不同進程就是指不同程序,aidl實現不同程序之間的調用。
②主線程與子線程通信使用handler,handler可以在子線程中發出消息,在主線程處理消息,從而完成線程之間的通信,即使有多個線程,仍然是一個程序。
③不同程序之間需要通過aidl通信,通信方式可以有多種,aidl是其中一種。實現的結果就像自己的程序調用自己的其他方法一樣,感覺就像一個程序。
④業務場景:例如購物app需要支付,購物app是淘寶,支付app是支付寶。所以就需要不同的程序進行通信。
二、首先介紹一個App之間的Service和Activity之間的通信
【項目結構】
【MyService】
【提示】
①創建Service
②如果不是通過上述方法創建,一定要記得注冊
1 <service 2 android:name=".MyService"
3 android:enabled="true"
4 android:exported="true"></service>
【代碼】
1 public class MyService extends Service { 2 public MyService() { 3 } 4
5 @Override 6 public IBinder onBind(Intent intent) { 7 return new MyBinder();//return MyBinder通過ServiceConnection在activity中拿到MyBinder
8 } 9
10 @Override 11 public int onStartCommand(Intent intent, int flags, int startId) { 12
13 return super.onStartCommand(intent, flags, startId); 14 } 15
16 public void payService(){ 17 Log.i("MyService", "payService: --------"); 18 } 19
20 class MyBinder extends Binder{ 21
22 public void pay(){ 23 payService(); 24 }//通過Binder實例將service中的方法暴露出去
25 } 26 }
【layout_main】
添加按鈕,點擊便於調用
1 <Button 2 android:id="@+id/btn_paly"
3 android:text="Pay"
4 android:layout_width="wrap_content"
5 android:layout_height="wrap_content" />
【MainActivity】
1 public class MainActivity extends AppCompatActivity { 2
3 MyService.MyBinder binder = null; 4 ServiceConnection conn; 5
6 @Override 7 protected void onCreate(Bundle savedInstanceState) { 8 super.onCreate(savedInstanceState); 9 setContentView(R.layout.activity_main); 10
11 Button btnPlay = (Button) findViewById(R.id.btn_paly); 12 conn = new ServiceConnection() { 13 @Override 14 public void onServiceConnected(ComponentName componentName, IBinder iBinder) { 15 binder = (MyService.MyBinder) iBinder; 16 } 17
18 @Override 19 public void onServiceDisconnected(ComponentName componentName) { 20
21 } 22 }; 23
24 Intent intent = new Intent(MainActivity.this,MyService.class); 25 bindService(intent,conn,BIND_AUTO_CREATE);//開啟服務
26
27 btnPlay.setOnClickListener(new View.OnClickListener() { 28 @Override 29 public void onClick(View view) { 30 if (binder!=null){ 31 binder.play(); 32 } 33 } 34 }); 35 } 36 }
【效果】
點擊后輸出service中pay方法中的內容
三、兩個App之間的Service通信
【項目結構】
【步驟】
①在AppPayProvider中創建MyService
代碼同上
【注冊】
Ⅰ、注冊時(android:enabled="true" android:exported="true")設置為true,將Service暴露出去,另一個App才能訪問到它
Ⅱ、添加『<intent-filter>』。由於不是同一個App,通過intent-filter對Intent進行過濾,讓另一個app通過action開啟服務
1 <service 2 android:name=".MyService"
3 android:enabled="true"
4 android:exported="true">
5 <!--enable:ture設置可用 6 exported:ture對外暴露 -->
7 <intent-filter>
8 <action android:name="com.xqz.apppayprovider.MyService" />
9 </intent-filter>
10 </service>
②MainActivity和layout_main保留創建時不作任何修改,但也不要刪掉,因為安裝程序必須提供起始頁面,否則將會出錯
③在AppPayProvider中添加AIDL
【代碼】
【提示】接口中定義中方法要和Service中的MyBinder中的方法一致
④再創建好AIDL,添加完方法后,android studio需要對這個aidl進行編譯,會自動按aidl規范生成一個Binder子類的代碼。
⑤對MyService中的MyBinder進行修改
【提示】繼承IPay.Stub。在這之前必須Make Project,否則將沒有只能聯想
⑥創建AppPayUser對AppPayProvider中的MyService進行操作
【layout-main】
1 <Button 2 android:id="@+id/btnPay"
3 android:text="pay"
4 android:layout_width="wrap_content"
5 android:layout_height="wrap_content" />
⑦將AppPayProvider中AIDL拷貝到AppPayUser中
【提示】Ⅰ、包名要相同,按目錄位置復制,通過下述方法,直接在文件夾進行復制。『此處可以查看項目結構,可以看到包名是相同的』
Ⅱ、同樣拷貝過來后需要Make Project
⑧【AppPayUser-MainActivity】
1 public class MainActivity extends AppCompatActivity { 2
3 Button btnPay; 4 private IPay myBinder;//定義AIDL
5
6 ServiceConnection conn = new ServiceConnection() { 7 @Override 8 public void onServiceConnected(ComponentName componentName, IBinder iBinder) { 9
10 myBinder = IPay.Stub.asInterface(iBinder); 11 } 12
13 @Override 14 public void onServiceDisconnected(ComponentName componentName) { 15
16 } 17 }; 18
19 @Override 20 protected void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 setContentView(R.layout.activity_main); 23
24 Intent intent = new Intent(); 25 intent.setAction("com.xqz.apppayprovider.MyService"); 26 //表示按照什么進行過濾,啟動意圖
27 /*android5.0之后,如果servicer不在同一個App的包中, 28 需要設置service所在程序的包名 29 (包名可以到App的清單文件AndroidManifest中查看)*/
30 intent.setPackage("com.xqz.apppayprovider"); 31 bindService(intent,conn,BIND_AUTO_CREATE);//開啟Service
32
33 btnPay = (Button) findViewById(R.id.btnPay); 34
35 btnPay.setOnClickListener(new View.OnClickListener() { 36 @Override 37 public void onClick(View view) { 38 try { 39 myBinder.pay(); 40 } catch (RemoteException e) { 41 //因為是跨程序調用服務,可能會出現遠程異常
42 e.printStackTrace(); 43 } 44 } 45 }); 46 } 47 }
【安裝】
先安裝AppPayProvider再安裝AppPayUser。
【效果】
將run中的 視圖調到AppPayProvider,點擊模擬器AppPayUser中的pay按鈕,將會執行AppPayProvider中MyService中pay方法中的內容。
四、總結
【跨App和同App之間的區別】
①跨App開啟服務是提供服務的App需要設置intent-filter過濾器,控制服務的App需要通過。setAction和setPackage方法進行設置action和包名,才能開啟服務。而同App只需要指定啟動的service就可。
②跨App的MyBinder實例要通過AIDL獲取,兩個應用定義同樣的接口的方法,通過對應的AIDL名稱.Stub.asInterface方法得到binder實例,然后就和同App的myBinder使用么有區別了。
③跨App的MyBinder對象的使用必須捕獲異常,而同App不需要。
④可以根據上方簡單的例子實現很多類似的功能。
五、園友實踐問題
1、Attempt to invoke interface method 'void com.example.aidl_.IPsy.pay()' on a null object
錯誤原因:提供遠程服務的應用和調用遠程服務的應用不是同一個應用,兩個應用使用的applicationId 相同。要想在同一個手機上安裝,他們得設置不同的 applicationId (buidle.gradle)
有關ApplicationId:applicationId默認為應用的包名,可以修改,也有相應的命名規則。在bindService intent 設置 setPackage 這里不是字面上的應用包名,而是應用的 ApplicationId,這也會導致 bindService 返回 false。 更多請參考博客:Gradle學習之設置applicationId( https://www.jianshu.com/p/4653c8a38771)
2、api 30 Android 11 bindservice 返回 false,在 API 28 (Android 9)的模擬器上是可以的 bindService 返回 true,但是運行到 API 30(Android 11)上就返回 false。
我剛開始解決問題的方向一直放在代碼上面,是為了兼容需要在代碼上做什么處理,看了官網並沒有得到什么信息。
園友說將 targetSDKVersion 設置為 28就可以了。
targetSDKVersion 設置應用的目標版本,當Android版本低於APP的目標的API時,將使用Android手機版本API即28,當APP目標版本低於Android手機版本,將以APP目標版本API在Android手機上以目標版本的行為運行,也就是 目標版本設置為 28 在api30 的手機上,以api 28行為運行,這也就是APP的向上兼容。