您的位置:首页 > 移动开发 > Android开发

Android Service 使用总结

2016-04-12 11:11 375 查看

1.Service:用于没有用户界面,但需要长时间在后台运行的应用。它类似于桌面应用或者服务器操作系统上的服务或守护进程。 Service 是在后台运行的可执行的代码块,从它被初始化一直运行到该程序关闭。一个 Service 的典型的例子是一个 MP3 播放器,尽管用户已经使用其他应用程序,但仍然需要持续播放文件。你的应用程序可能需要在没有用户界面的情况下一直执行 Service 来实现后台任务,及可以理解为:Service组件通常用于为其他组件提供后台服务或监控其他组件的运行状态。每个Service必须在manifest中
通过<service></service>来声明。

<service
android:name=".MyService"> //声明一个名为MyService的Service

<intent-filter>

<!-- 为该Service组件的intent-filter配置action -->

<action
android:name="com.jph.servicedemo.BIND_SERVICE"></action>

</intent-filter>

</service>

1.startService()方式:当应用程序组件(如activity)调用startService()方法启动服务时,服务处于started状态。当服务是started状态时,其生命周期与启动它的组件无关,并且可以在后台无限期运行,即使启动服务的组件已经被销毁。因此,服务需要在完成任务后调用stopSelf()方法停止,或者由其他组件调用stopService()方法停止。

2.bindService()方式:调用者与服务绑定在了一起,调用者一旦退出,服务也就终止,大有“不求同时生,必须同时死”的特点。

Service启动方法:

Context.startService(Intent intent)

step1.新建一个类MyService,继承Service

step2.在Manifest.xml文件中声明<service android:name=".MyService"/>

private
Context mContext;
Intent i =
new Intent();

i.setClass(ServiceDemo.this,
MyService.class);

mContext.startService(i);

Context.stopService(Intent intent) //停止方法

Intent i =
new Intent();

i.setClass(ServiceDemo.this,
MyService.class);

mContext.stopService(i);

Context.bindService(Intent intent, ServiceConnection conn, int flags)

Intent:是跳转到service的intent,如Intent intent = new Intent(); intent.setClass(this,MyService.class);

conn:则是一个代表与service连接状态的类,当我们连接service成功或失败时,会主动触发其内部的onServiceConnectedonServiceDisconnected方法。如果我们想要访问service中的数据,可以在onServiceConnected()方法中进行实现,

step1.新建一个类MyService,继承Service

step2.在Manifest.xml文件中声明<service android:name=".MyService"/>

step3.绑定

Intent intent = new Intent(context,MyService.class);
//conn中onServiceConnected()方法实现bindService后做的事情

ServiceConnection conn = new ServiceConnection(){
@Override

public void onServiceConnected(ComponentName name,
IBinder service) {

myService
= ((MyService.MyBinder) service).getService();

textView.setText("i am from service:"
+ myService.getSystemTime());//

}

@Override

public void onServiceDisconnected(ComponentName name) {

}
}

context.bindService(intent,conn,Context.BIND_AUTO_CREATE);

mContext.unbindService(conn);//关闭邦定

来自其他网络摘要

生命周期:
使用context.startService() 启动Service是会会经历:

  context.startService() ->onCreate()- >onStart()->Service running

  context.stopService() | ->onDestroy() ->Service stop

  如果Service还没有运行,则android先调用onCreate()然后调用onStart();如果Service已经运行,则只调用onStart(),所以一个Service的onStart方法可能会重复调用多次。

  stopService的时候直接onDestroy,如果是调用者自己直接退出而没有调用stopService的话,Service会一直在后台运行。该Service的调用者再启动起来后可以通过stopService关闭Service。

  所以调用startService的生命周期为:onCreate --> onStart(可多次调用) --> onDestroy
  使用使用context.bindService()启动Service会经历:

 context.bindService()->onCreate()->onBind()->Service running

  onUnbind() -> onDestroy() ->Service stop

  onBind将返回给客户端一个IBind接口实例,IBind允许客户端回调服务的方法,比如得到Service运行的状态或其他操作。这个时候把调用者(Context,例如Activity)会和Service绑定在一起,Context退出了,Srevice就会调用onUnbind->onDestroy相应退出。

 所以调用bindService的生命周期为:onCreate --> onBind(只一次,不可多次绑定) --> onUnbind --> onDestory。

  在Service每一次的开启关闭过程中,只有onStart可被多次调用(通过多次startService调用),其他onCreate,onBind,onUnbind,onDestory在一个生命周期中只能被调用一次。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: