android外掛程式化-apkplug中OSGI服務基本原理-08

來源:互聯網
上載者:User

 

一 OSGI與android Service 異同點

OSGI服務與android Service概念差不多也是Service ,Client 關係。

android Service介面 --service.AIDL

OSGI介面 --java interface

所以android 處理序間通訊Service只能傳遞序列化過的資料 而OSGI服務可以傳遞任何java對象。

 

二 OSGI與android Service註冊/查詢方式對比

1.服務註冊

android Service

1 Intent intent=new Intent(Context,Service.class);
2 Context.startService(intent);

OSGI Service

 

1 BundleContext context; //外掛程式上下文
2 ServiceRegistration m_reg = context.registerService(
3 sayHelloImp.class.getName(),//服務名稱 一般為介面類名
4 my, //服務具體實作類別
5 null);

2.服務查詢

android Service

1 Intent intent=new Intent(Context,Service.class);
2 Context.bindService(intent, new ServiceConnection())
3 ...

OSGI Service

 

01 //利用外掛程式上下文BundleContext查詢服務
02 ServiceReference ref = context.getServiceReference(Service.class.getName());
03 if (ref != null ) {
04 //尋找到服務
05 Service service = (Service) context.getService(ref);
06 if (service != null ) {
07 //調用服務介面
08 service.sayHello(imp);
09 }
10 //登出服務
11 context.ungetService(ref);
12 }

 

三 OSGI服務特點

OSGI服務是暫態的外掛程式可能隨時被關閉或卸載,所以我們每次在使用服務的時候都最好先查詢服務是否還存在。

四 OSGI服務注意事項

使用OSGI服務時應注意服務介面java類的一致性,服務者與消費者應使用相同的java介面(類載入器相同),否則可能出現服務查詢時類型強制轉換異常。一般情況下我們以服務者提供java介面

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.