一、項(xiàng)目介紹
【知識(shí)準(zhǔn)備】
①Android Interface definition language(aidl,android接口定義語(yǔ)言),其目的實(shí)現(xiàn)跨進(jìn)程的調(diào)用。進(jìn)程是程序在os中執(zhí)行的載體,一個(gè)程序?qū)?yīng)一個(gè)進(jìn)程,不同進(jìn)程就是指不同程序,aidl實(shí)現(xiàn)不同程序之間的調(diào)用。
②主線程與子線程通信使用handler,handler可以在子線程中發(fā)出消息,在主線程處理消息,從而完成線程之間的通信,即使有多個(gè)線程,仍然是一個(gè)程序。
③不同程序之間需要通過(guò)aidl通信,通信方式可以有多種,aidl是其中一種。實(shí)現(xiàn)的結(jié)果就像自己的程序調(diào)用自己的其他方法一樣,感覺(jué)就像一個(gè)程序。
④業(yè)務(wù)場(chǎng)景:例如購(gòu)物app需要支付,購(gòu)物app是淘寶,支付app是支付寶。所以就需要不同的程序進(jìn)行通信。
二、首先介紹一個(gè)App之間的Service和Activity之間的通信
【項(xiàng)目結(jié)構(gòu)】
【MyService】
【提示】
①創(chuàng)建Service
②如果不是通過(guò)上述方法創(chuàng)建,一定要記得注冊(cè)
1
2
3
4
5
6
|
<service android:name= ".MyService" android:enabled= "true" android:exported= "true" > </service> |
【代碼】
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
public class MyService extends Service { public MyService() { } @Override public IBinder onBind(Intent intent) { return new MyBinder(); //return MyBinder通過(guò)ServiceConnection在activity中拿到MyBinder } @Override public int onStartCommand(Intent intent, int flags, int startId) { return super .onStartCommand(intent, flags, startId); } public void payService(){ Log.i( "MyService" , "payService: --------" ); } class MyBinder extends Binder{ public void pay(){ payService(); } //通過(guò)Binder實(shí)例將service中的方法暴露出去 } } |
【layout_main】
添加按鈕,點(diǎn)擊便于調(diào)用
1
2
3
4
5
6
|
<Button android:id= "@+id/btn_paly" android:text= "Pay" android:layout_width= "wrap_content" android:layout_height= "wrap_content" /> |
【MainActivity】
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
public class MainActivity extends AppCompatActivity { MyService.MyBinder binder = null ; ServiceConnection conn; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btnPlay = (Button) findViewById(R.id.btn_paly); conn = new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { binder = (MyService.MyBinder) iBinder; } @Override public void onServiceDisconnected(ComponentName componentName) { } }; Intent intent = new Intent(MainActivity. this ,MyService. class ); bindService(intent,conn,BIND_AUTO_CREATE); //開啟服務(wù) btnPlay.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { if (binder!= null ){ binder.play(); } } }); } } |
【效果】
點(diǎn)擊后輸出service中pay方法中的內(nèi)容
三、兩個(gè)App之間的Service通信
【項(xiàng)目結(jié)構(gòu)】
【步驟】
①在AppPayProvider中創(chuàng)建MyService
代碼同上
【注冊(cè)】
Ⅰ、注冊(cè)時(shí)(android:enabled="true" android:exported="true" )設(shè)置為true,將Service暴露出去,另一個(gè)App才能訪問(wèn)到它
Ⅱ、添加『 <intent-filter> 』。由于不是同一個(gè)App,通過(guò)intent-filter對(duì)Intent進(jìn)行過(guò)濾,讓另一個(gè)app通過(guò)action開啟服務(wù)
1
2
3
4
5
6
7
8
9
10
|
<service android:name= ".MyService" android:enabled= "true" android:exported= "true" > <!--enable:ture設(shè)置可用 exported:ture對(duì)外暴露 --> <intent-filter> <action android:name= "com.xqz.apppayprovider.MyService" /> </intent-filter> </service> |
②MainActivity和layout_main保留創(chuàng)建時(shí)不作任何修改,但也不要?jiǎng)h掉,因?yàn)榘惭b程序必須提供起始頁(yè)面,否則將會(huì)出錯(cuò)
③在AppPayProvider中添加AIDL
【代碼】
【提示】接口中定義中方法要和Service中的MyBinder中的方法一致
④再創(chuàng)建好AIDL,添加完方法后,android studio需要對(duì)這個(gè)aidl進(jìn)行編譯,會(huì)自動(dòng)按aidl規(guī)范生成一個(gè)Binder子類的代碼。
⑤對(duì)MyService中的MyBinder進(jìn)行修改
【提示】繼承IPay.Stub。在這之前必須Make Project,否則將沒(méi)有只能聯(lián)想
⑥創(chuàng)建AppPayUser對(duì)AppPayProvider中的MyService進(jìn)行操作
【layout-main】
1
2
3
4
5
|
<Button android:id= "@+id/btnPay" android:text= "pay" android:layout_width= "wrap_content" android:layout_height= "wrap_content" /> |
⑦將AppPayProvider中AIDL拷貝到AppPayUser中
【提示】Ⅰ、包名要相同,按目錄位置復(fù)制,通過(guò)下述方法,直接在文件夾進(jìn)行復(fù)制。『此處可以查看項(xiàng)目結(jié)構(gòu),可以看到包名是相同的』
Ⅱ、同樣拷貝過(guò)來(lái)后需要Make Project
⑧【AppPayUser-MainActivity】
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
public class MainActivity extends AppCompatActivity { Button btnPay; private IPay myBinder; //定義AIDL ServiceConnection conn = new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { myBinder = IPay.Stub.asInterface(iBinder); } @Override public void onServiceDisconnected(ComponentName componentName) { } }; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent = new Intent(); intent.setAction( "com.xqz.apppayprovider.MyService" ); //表示按照什么進(jìn)行過(guò)濾,啟動(dòng)意圖 /*android5.0之后,如果servicer不在同一個(gè)App的包中, 需要設(shè)置service所在程序的包名 (包名可以到App的清單文件AndroidManifest中查看)*/ intent.setPackage( "com.xqz.apppayprovider" ); bindService(intent,conn,BIND_AUTO_CREATE); //開啟Service btnPay = (Button) findViewById(R.id.btnPay); btnPay.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { try { myBinder.pay(); } catch (RemoteException e) { //因?yàn)槭强绯绦蛘{(diào)用服務(wù),可能會(huì)出現(xiàn)遠(yuǎn)程異常 e.printStackTrace(); } } }); } } |
【安裝】
先安裝AppPayProvider再安裝AppPayUser。
【效果】
將run中的 視圖調(diào)到AppPayProvider,點(diǎn)擊模擬器AppPayUser中的pay按鈕,將會(huì)執(zhí)行AppPayProvider中MyService中pay方法中的內(nèi)容。
四、總結(jié)
【跨App和同App之間的區(qū)別】
①跨App開啟服務(wù)是提供服務(wù)的App需要設(shè)置intent-filter過(guò)濾器,控制服務(wù)的App需要通過(guò)。setAction和setPackage方法進(jìn)行設(shè)置action和包名,才能開啟服務(wù)。而同App只需要指定啟動(dòng)的service就可。
②跨App的MyBinder實(shí)例要通過(guò)AIDL獲取,兩個(gè)應(yīng)用定義同樣的接口的方法,通過(guò)對(duì)應(yīng)的AIDL名稱.Stub.asInterface方法得到binder實(shí)例,然后就和同App的myBinder使用么有區(qū)別了。
③跨App的MyBinder對(duì)象的使用必須捕獲異常,而同App不需要。
④可以根據(jù)上方簡(jiǎn)單的例子實(shí)現(xiàn)很多類似的功能。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://www.cnblogs.com/xqz0618/p/aidl_service.html