android插件技术-apkplug于OSGI服务基础-08

本文详细对比了OSGi服务与AndroidService的区别,包括注册、查询方式,并介绍了OSGi服务的特点和注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们提供 apkplug 下OSGI使用demo 源代码托管地址为 http://git.oschina.net/plug/OSGIService

一 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介面

版权声明:本文博主原创文章,博客,未经同意不得转载。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值