现在的位置: 首页 > 综合 > 正文

Android Service 解析

2017年08月15日 ⁄ 综合 ⁄ 共 4561字 ⁄ 字号 评论关闭

Service简述

  Service是运行在后台的,没有界面的,用来处理耗时比较长的。Service不是一个单独的进程,不是一个单独的线程。

  Service有两种类型:

    本地服务(Local Service):用于应用程序内部 

    远程服务(Remote Sercie):用于android系统内部的应用程序之间 

本地服务用于实现应用程序自己的一些耗时任务,比如查询升级信息,并不占用应用程序比如Activity所属线程,而是单

开线程后台执行,这样用户体验比较 好。

远程服务可被其他应用程序复用,比如天气预报服务,其他应用程序不需要再写这样的服务,调用已有的即可。


服务的生命周


Service的启动方法

服务不能自己运行,需要通过调用Context.startService() 或 Context.bindService()方法启动服务。这两个方法都可以启

动Service,但是它们的使用场合有所不同。

使用startService()方法启用服务,调用者与服务之间没有关连,即使调用者退出了,服务仍然运行。

使用bindService()方法启用服务,调用者与服务绑定在了一起,调用者一旦退出,服务也就终止,大有“不求同生,但

求同死”的特点。 

1 通过startService()

  onCreate --> onStartCommand()--> onDestroy()  

    startService(): onCreate --> onStartCommand()

    stopService:onDestroy() 

  采用Context.startService()方法启动服务,在服务未被创建时,系统会先调用服务的onCreate()方法.

  接着调用onStart()方法。如果调用startService()方法前服务已经被创建,多次调用startService()方法并不会导致多次

创建服务,但会导致多次调用onStart()方法。

  采用startService()方法启动的服务,只能调用Context.stopService()方法结 束服务,服务结束时会调用onDestroy()方

法。


2 通过bindService()  

  onCreate()-->onBind()--> onUnbind()-->onDestroyed() 

    bindService():调用者和Service绑定在一起 

    unbindService():  onUnbind()-->onDestroyed() 

  采用Context.bindService()方法启动服务,在服务未被创建时,系统会先调用服务的onCreate()方法,

  接着调用onBind()方法。这个时候调用者和服务绑定在一起,调用者退出了,系统就会先调用服务的onUnbind()方

法,接着调用onDestroy()方法。

  如调用bindService()方法前服务已经被绑定,多次调用bindService()方法并不会导致多次创建服务及绑定(也就是说

onCreate()和onBind()方法并不会被多次调用)。

  如调用者希望与正在绑定的服务解除绑定,可调用unbindService()方法,调用该方法会导致系统调用的 onUnbind()--

>onDestroy()方法. 


3 bindService方法的说明 

bindService(Intent service,Service Connection conn,int flags):

  Service:通过Intent指定要启动的service

  Conn:一个Service Connection对象,该对象用来监听访问者与service的连接情况。

    如果连接成功则会回调该对象的:onServiceConnected()方法。

    当断开连接就回调该对象的:onServiceDisconnected()方法

  Flags:指定绑定是否自动创建service,值为0或BIND_AUTO_CREATE

  onServiceConnected(ComponentName name, IBinder service):  service:通过这个Ibinder对象实现与service通信,在

开发service类时,该类必须实现一个Ibinder onBind(Intent intent)的方法,该方法返回的Ibinder对象就会传递给service

参数,这样访问者就可以和service通信了

startService() 实例代码如下:

Activity:

@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		intent=new Intent("cn.edu.zwu.tel.lgs001");
	}

	public void onStartClick(View view)
	{
		startService(intent);
	}
	
	public void onStopClick(View view)
	{
		stopService(intent);
	}

Service:

public class myService extends Service {
	@Override
	public IBinder onBind(Intent arg0) {
		System.out.println("onBind mathod is called");
		return null;
	}

	@Override
	public void onCreate() {
		System.out.println("onCreate mathod is called");
		super.onCreate();
	}

	@Override
	public void onDestroy() {
		System.out.println("onDestroy mathod is called");
		super.onDestroy();
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		System.out.println("onStartCommand mathod is called");
		return super.onStartCommand(intent, flags, startId);
		// return START_STICKY;
	}

	@Override
	public void onRebind(Intent intent) {
		System.out.println("onRebind mathod is called");
		super.onRebind(intent);
	}

	@Override
	public boolean onUnbind(Intent intent) {
		System.out.println("onUnbind mathod is called");
		return super.onUnbind(intent);
	}
}

AndroidManifest.xml:

 <service android:name=".myService">
            <intent-filter >
                <action android:name="cn.edu.zwu.tel.lgs001"/>
            </intent-filter>
        </service>

bindService()   实例代码如下:

Activity:

private myService2 ms2;
   private ServiceConnection conn=new ServiceConnection()
    {

		@Override
		public void onServiceConnected(ComponentName arg0, IBinder arg1) {
			ms2=((myService2.myBinder)arg1).getService();
			textView.setText(ms2.setString());
		}

		@Override
		public void onServiceDisconnected(ComponentName arg0) {
			ms2=null;
		}
    };

@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		intent2=new Intent("cn.edu.zwu.tel.lgs002");
		textView=(TextView)findViewById(R.id.textId);
	}

        public void onBindClick(View view)
	{
		bindService(intent2, conn, Context.BIND_AUTO_CREATE);
	}
	
	public void onUnBindClick(View view)
	{
		unbindService(conn);
	}

myService2:

public class myService2 extends Service {	
    private myBinder binder=new myBinder();
    public class myBinder extends Binder 
    {
		myService2 getService()
		{
			return myService2.this;
		}
	}
    
	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return binder;
	}


	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
	}


	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}


	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		return super.onStartCommand(intent, flags, startId);
	}


	@Override
	public boolean onUnbind(Intent intent) {
		// TODO Auto-generated method stub
		return super.onUnbind(intent);
	}
	
    public String setString()
    {
    	return "绑定了";
    }
}


AndroidManifest.xml:

      <service android:name=".myService2">
           <intent-filter >
               <action android:name="cn.edu.zwu.tel.lgs002"/>
           </intent-filter>
      </service>

【上篇】
【下篇】

抱歉!评论已关闭.