Android Service

来源:互联网 发布:sql和动软代码生成器 编辑:程序博客网 时间:2024/05/16 06:26

Service与Activity的级别差不多,运行在后台,不与用户交互。Service是在主线程中,要在Service中运行好事的代码的话,为了不阻塞UI线程应该新创建一个线程。创建一个Service需要继承自Service:

package com.zhychengsssd;import android.app.Service;import android.content.Intent;import android.media.MediaPlayer;import android.os.IBinder;public class MyService extends Service{MediaPlayer mp=null;@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubSystem.out.println("------>>onBind");return null;}@Overridepublic void onCreate() {// TODO Auto-generated method stubsuper.onCreate();System.out.println("------>>onCreate");}@Overridepublic void onStart(Intent intent, int startId) {// TODO Auto-generated method stubsuper.onStart(intent, startId);mp=MediaPlayer.create(this, R.raw.a);mp.start();System.out.println("------>>onStart");System.out.println("------->startId"+startId);}@Overridepublic void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();if(mp!=null)mp.stop();mp=null;System.gc();System.out.println("------>>onDestroy");}@Overridepublic boolean onUnbind(Intent intent) {// TODO Auto-generated method stubSystem.out.println("------>>onUnbind");return super.onUnbind(intent);}@Overridepublic void onRebind(Intent intent) {// TODO Auto-generated method stubsuper.onRebind(intent);System.out.println("------>>onRebind");}    }

覆盖Service中的各种方法。不要忘了注册Service。

<service android:name="MyService">            <intent-filter>                <action android:name="zhycheng"/>                            </intent-filter>        </service>

在这里加了一个intent-filter和action标签的话就允许使用Service的action的name来启动Service。

启动Service有如下方法

Intent intent=new Intent(this,MyService.class);this.startService(intent);

也可以使用如下方法

Intent intent=new Intent(this,MyService.class);this.startService(new Intent("zhycheng"));

使用者两个方法启动的Service必须使用如下方法才能停止Service

Intent intent=new Intent(this,MyService.class);this.stopService(intent);

但是使用下面的方法启动的Service这与Context绑定,Context退出了,Srevice就会调用onUnbind->onDestroy相应退出。

Intent intent=new Intent(this,MyService.class);this.bindService(intent, sc, Context.BIND_AUTO_CREATE);

sc是ServiceConnection的对象,覆盖方法可以控制:

ServiceConnection sc=new ServiceConnection(){@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {// TODO Auto-generated method stub}@Overridepublic void onServiceDisconnected(ComponentName name) {// TODO Auto-generated method stub}};

在连接了Service的时候的操作和断开连接的操作。

当调用取消绑定则Service退出

Intent intent=new Intent(this,MyService.class);this.unbindService(sc);

Service是android 系统中的一种组件,它跟Activity的级别差不多,但是他不能自己运行,只能后台运行,并且可以和其他组件进行交互。Service的启动有两种方式:context.startService()context.bindService()。

使用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

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

在Service每一次的开启关闭过程中,只有onStart可被多次调用(通过多次startService调用),其他onCreate,onBind,onUnbind,onDestory在一个生命周期中只能被调用一次。

如果这两种方法交织使用,有如下规则

1.onCreate,onBind只执行一次。

2.先使用start再使用bind则直接执行onBind,先使用bind,后使用start则执行onStart

3.先bind再stop就停止不了,必须先用unBind停止。

4.先start再unBind,就出错,应为你没有bind,怎么能unbind?

service可以在和多场合的应用中使用,比如播放多媒体的时候用户启动了其他Activity这个时候程序要在后台继续播放,比如检测SD卡上文件的变化,再或者在后台记录你地理信息位置的改变等等,总之服务嘛,总是藏在后头的。

原创粉丝点击