安卓:service,启动service和绑定service生命周期

一。

startService:

生命周期为:


逻辑代码文件:

package com.example.day22_startservice_life;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void click(View v)
    {
    	switch(v.getId())
    	{
    	case R.id.bt1:
    		Intent intent=new Intent(MainActivity.this, MyService.class);
    		startService(intent);
    		break;
    	case R.id.bt2:
    		Intent intent2=new Intent(MainActivity.this, MyService.class);
    		stopService(intent2);
    		break;
    	}
    }
}


继承service的类文件:

package com.example.day22_startservice_life;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class MyService extends Service{

	String tag="##MyService##";
	@Override
	public IBinder onBind(Intent intent) {
		Log.i(tag, "==onBind()==");
		return null;
	}
	@Override
	public void onCreate() {
		super.onCreate();
		Log.i(tag, "==onCreate()==");
	}
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		Log.i(tag, "==onStartCommand()==");
		return START_NOT_STICKY;
	}
	@Override
	public void onDestroy() {
		super.onDestroy();
		Log.i(tag, "==onDestroy()==");

	}
}


布局文件:



    



二。

bindService:

生命周期为:

  ----》  

逻辑代码文件:

package com.example.day22_service3;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;

public class MainActivity extends Activity {

	MyServiceConn conn=new MyServiceConn();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void click(View v)
    {
    	switch(v.getId())
    	{
    	case R.id.bt1:
    		Intent intent=new Intent(MainActivity.this,MyService.class);
    		/*
    		 * 参数1:当前绑定服务的intent对象,指明绑定组件
    		 * 参数2:当前服务的连接状态
    		 * 参数3:绑定服务的一个标记,绑定并且启动
    		 */
    		bindService(intent, conn, Context.BIND_AUTO_CREATE);
    		break;
    	case R.id.bt2:
    		unbindService(conn);
    		break;
    	}
    }
    
    class MyServiceConn implements ServiceConnection
    {
    	//服务连接成功后回调的方法
    	/*
    	 * 参数1:组件的名称
    	 * 参数2:连接后onBind()返回的值
    	 */
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			
		}
    	    	
    }
    
}


继承Service的类文件:

package com.example.day22_service3;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class MyService extends Service{

	String tag="##MyService##";
	//通过bindService绑定服务,启动后回调的方法
	@Override
	public IBinder onBind(Intent intent) {
		Log.i(tag, "==onBind==");
		return null;
	}
	//服务创建时回调的方法
	@Override
	public void onCreate() {
		super.onCreate();
		Log.i(tag, "==onCreate==");

	}
	//通过unbindService解绑时回调的方法
	@Override
	public boolean onUnbind(Intent intent) {
		Log.i(tag, "==onUnbind==");

		return super.onUnbind(intent);
	}
	//有新的client连接到当前的服务
	@Override
	public void onRebind(Intent intent) {
		super.onRebind(intent);
		Log.i(tag, "==onRebind==");

	}
	//服务销毁时回调的方法
	@Override
	public void onDestroy() {
		super.onDestroy();
		Log.i(tag, "==onDestroy==");

	}
}


布局文件:


	



你可能感兴趣的:(安卓service)