随笔 - 4633  文章 - 0  评论 - 22  阅读 - 185万

Service熟人

Service 为一体的四个分量间(剩余有Activity ,内容提供商,广播),它属于后台工作,能够在后台很长一段时间执行,他没有接口。

首先从使用方式上来说来说

他有两种使用方式:

1、启动式使用Service

    启动:context.startService()

    结束:Service.stopSelf()

    它具有自我管理的能力。不须要通过函数调用向外部组件提供数据和功能

2、绑定式使用Serviceo

    1、通过服务连接(Connection)或直接获取Service状态,和数据信息

   2、服务连接能够获取Service对象,所以绑定Service的组件能够调用Service中实现的方法

  3、建立连接Context.bindService,停止服务连接:Context.unbindService()

 生命周期。生命周期还是直接看API 上的图比較清晰


启动Service分为显示启动和隐式启动两种启动方式

隐式启动
<service android:name=".service">
<intent-filer>
<action android:name="com.android.service"/>
<intent-filer>
</service>
final Intent serviceIntent=new Intent();
serviceIntent.setAction("com.android.service");

startService(serviceIntent);
显示启动
final Intent serviceIntent=new Intent(this,service.class);
startService(serviceIntent);
假设在同一个包中。

两者都可以使用。在不同的包。

仅隐式启动

这些仅仅是一个Service一些基本的知识表示



posted on   gcczhongduan  阅读(145)  评论(0编辑  收藏  举报
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

点击右上角即可分享
微信分享提示