Android开发之低调的Service

锲而舍之,朽木不折;锲而不舍,金石可镂。——荀况

今天学习了一下Service的用法就和大家一起来讨论Android中Service的相关知识点,如有谬误,欢迎批评指正,如有疑问欢迎留言。

一、Service用途

Service在Android中和Activity是属于同一级别上的组件,Android中的Service,其意思是“服务”,它在后台运行不可交互。Service自己不能运行,需要通过某一个Activity或者其它Context对象来调用,Context.startService()和Context.bindService()两种方式启动 Service 。 Service在Android中和Activity是属于同一级别上的组件,Android 中的Service ,其意思是“服务”,它是在后台运行,不可交互的。Service自己不能运行,需要通过某一个Activity或者其它Context对象来调用,Context.startService()和Context.bindService()两种方式启动 Service 。 Android 中的服务,它与 Activity不同,它是不能与用户交互的,不能自己启动的,运行在后台的程序(干着重的工作,却连个界面也没有,因此我说它低调),如果我们退出应用时, Service进程并没有结束,它仍然在后台运行,那我们什么时候会用到Service呢?比如我们播放音乐的时候,有可能想边听音乐边干些其他事情,当我们退出播放音乐的应用,如果不用 Service,我们就听不到歌了,所以这时候就得用到Service了,又比如当我们一个应用的数据是通过网络获取的,不同时间(一段时间)的数据是不同的,这时候我们可以用 Service在后台定时更新,而不用每打开应用的时候在去获取。如果在 Service的 onCreate或者 onStart方法中做一些很耗时的动作,最好是启动一个新线程来运行这个 Service,因为,如果 Service运行在主线程中,会影响到程序的 UI操作或者阻塞主线程中的其它事情。

二、Service的生命周期

首先来看官网给出的Service的生命周期图

3.onStartCommand(Intent intent,intflags,intstartId)启动Service

从这个生命周期图中我们可以看到有两种启动Service的方法Context.startService和Context.bindService,对应的生命周期也分为两种情况:

(1)startService启动模式

从图中可以看出当我们采用Context.startService(intent)这种方式时,系统会实例化一个服务,依次调用onCreate()和onStartCommand()方法,之后服务就进入了运行状态,如果服务已经运行了我们再次启动时不会重新创建服务,系统会自动调用刚才我们启动的服务,并调用其onStart()方法,如果我们想销毁一个服务可以使用stopService(intent)方法,使用stopService()方法会调用onDestroy()方法此时服务就被销毁了,

(2)bindService启动模式

在这种模式下,调用bindService(Intent service, ServiceConnection conn, int flags)来绑定一个Service,这时Service会调用自身的onCreate()方法(前提是该Service未创建),系统会实例化一个服务,接着调用onBind(intent)方法调用onBind方法后调用者就可以和服务进行交互了,当我们采用bindService这种方法创建服务时,如果已经创建好了一个如果再进行创建,系统不会创建新的Service实例,也不会调用onBind方法,这种方式启动的服务的销毁方法是使用unbindService方法,此时onUnbind方法和onDestroy方法都会被调用。

关于bindService(Intent service, ServiceConnection conn, int flags)参数的说明

参数①service: Intent 对象

参数②conn: ServiceConnection对象,实现其onServiceConnected()和onServiceDisconnected()在连接成功和断开连接时处理。后面有实例来进行说明

参数③flags:Service创建的方式,一般用Service.BIND_AUTO_CREATE表示绑定时自动创建。

可能有的人会问这两种启动模式有什么不同?

strarService和bindService的不同之处:startService模式下调用者与服务无必然联系,即使调用者结束了自己的生命周期,只要没有使用stopService方法停止这个服务,服务仍会运行;然而通常情况下,bindService模式下服务是与调用者同生共死的,在绑定结束之后,一旦调用者被销毁,服务就会终止,我们通常用一句话来形容bindService:不求同生,但求同死。

另外需要注意的是在Android2.0之前我们使用startService启动服务时都是习惯重写onStart方法,在Android2.0时系统引进了onStartCommand方法取代了onStart方法,但是为了兼容以前的程序,在onStartCommand方法中其实是调用了onStart方法,我们之后会做验证,不过我们最好还是重写onStartCommand方法。

小平同志曾经说过,实践是检验真理的唯一标准,下面我们就结合实例对上面的理论来做验证,以便加深印象

布局如下:

代码如下:

1.Service的代码

package com.example.servicepractice;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import android.util.Log;public class MyService extends Service {private static final String TAG = "MyService";@Overridepublic void onCreate() {super.onCreate();Log. i(TAG,"onCreate called" );}@Overridepublic void onStart(Intent intent, int startId) {super. onStart(intent, startId);Log. i(TAG,"onStart called" );}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log. i(TAG,"onStartCommand called" );return super.onStartCommand(intent, flags, startId);}@Overridepublic void onDestroy() {super.onDestroy();Log. i(TAG,"onDestroy called" );}@Overridepublic IBinder onBind(Intent intent) {Log. i(TAG,"onBind called" );return null;}@Overridepublic boolean onUnbind(Intent intent) {Log. i(TAG,"onUnbind called" );return super.onUnbind(intent);}}2.MainActivity的代码package com.example.servicepractice;import android.os.Bundle;import android.os.IBinder;import android.app.Activity;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;import android.util.Log;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup.LayoutParams;import android.widget.Button;public class MainActivity extends Activity {protected static final String TAG = "MyService";private Button btn_start;private Button btn_stop;private Button btn_bind;private Button btn_unbind;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout. activity_main);findViews();setClick();}private void findViews() {btn_start=(Button) findViewById(R.id. btn_start);btn_stop=(Button) findViewById(R.id. btn_stop);btn_bind=(Button) findViewById(R.id. btn_bind);btn_unbind=(Button) findViewById(R.id. btn_unbind);}private void setClick() {//采用startService启动服务btn_start.setOnClickListener( new OnClickListener() {public void onClick(View v) {Intent intent= new Intent(MainActivity.this,MyService.class );startService(intent);}});//销毁服务btn_stop.setOnClickListener( new OnClickListener() {public void onClick(View v) {Intent intent= new Intent(MainActivity.this,MyService.class );stopService(intent);}});//绑定服务btn_bind.setOnClickListener( new OnClickListener() {public void onClick(View v) {Intent intent = new Intent(MainActivity.this,MyService.class );bindService(intent, conn,Context. BIND_AUTO_CREATE);}});//解除绑定btn_unbind.setOnClickListener( new OnClickListener() {public void onClick(View v) {unbindService( conn);}});}private ServiceConnection conn=new ServiceConnection() {public void onServiceConnected(ComponentName name, IBinder service) {//connectedLog. i(TAG,"onServiceConnection called." );}public void onServiceDisconnected(ComponentName name) {} }; }而是他们在同伴们都睡着的时候,一步步艰辛地

Android开发之低调的Service

相关文章:

你感兴趣的文章:

标签云: