Android Service

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;
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		System.out.println("------>>onBind");
		return null;
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		
		super.onCreate();
		System.out.println("------>>onCreate");
	}

	@Override
	public void onStart(Intent intent, int startId) {
		// TODO Auto-generated method stub
		super.onStart(intent, startId);
		
		mp=MediaPlayer.create(this, R.raw.a);
		mp.start();
		System.out.println("------>>onStart");
		System.out.println("------->startId"+startId);
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if(mp!=null)
		mp.stop();
		mp=null;
		System.gc();
		System.out.println("------>>onDestroy");
	}

	@Override
	public boolean onUnbind(Intent intent) {
		// TODO Auto-generated method stub
		System.out.println("------>>onUnbind");
		return super.onUnbind(intent);
		
	}

	@Override
	public void onRebind(Intent intent) {
		// TODO Auto-generated method stub
		super.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(){

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public 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卡上文件的变化,再或者在后台记录你地理信息位置的改变等等,总之服务嘛,总是藏在后头的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值