一 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(),//服務名稱 一般為介面類名 |
2.服務查詢
android Service
1 |
Intent intent=new Intent(Context,Service.class); |
2 |
Context.bindService(intent, new ServiceConnection()) |
OSGI Service
01 |
//利用外掛程式上下文BundleContext查詢服務 |
02 |
ServiceReference ref = context.getServiceReference(Service.class.getName()); |
05 |
Service service = (Service) context.getService(ref); |
06 |
if (service != null ) { |
08 |
service.sayHello(imp); |
11 |
context.ungetService(ref); |
三 OSGI服務特點
OSGI服務是暫態的外掛程式可能隨時被關閉或卸載,所以我們每次在使用服務的時候都最好先查詢服務是否還存在。
四 OSGI服務注意事項
使用OSGI服務時應注意服務介面java類的一致性,服務者與消費者應使用相同的java介面(類載入器相同),否則可能出現服務查詢時類型強制轉換異常。一般情況下我們以服務者提供java介面