Service生命周期的学习

Service生命周期的学习_第1张图片

Service生命周期的学习_第2张图片


有两个方法使用service。

方法1是使用Context.startService()会启动service,如果service没有创建,首先会调用onCreate()方法,然后调用onStartCommand(Intent,int,int)方法,service会一直存在直到stopService()或stopSelf()调用。
方法2是使用Context.bindService(),如果service没有创建,首先也会调用onCreate()方法,但是如果已经创建则会调用onBind(Intent)方法返回客户端一个IBinder接口。

未调用stopService来停止服务,这个服务就会随着Android系统的启动而启动,随着Android系统的关闭而关闭。也就是服务会在Android系统启动后一直在后台运行,直到Android系统关闭后服务才停止。但有时我们希望在启动服务的Activity关闭后服务自动关闭,这就需要将Activity和Service绑定。

关闭应用程序后,会看到在Sysout视图中输出了onUnbind和onDestroy信息,表明在关闭Activity后,服务先被解除绑定,最后被销毁。

如果先启动(调用startService方法)一个服务,然后再绑定(调用bindService方法)服务,会怎么样呢?在这种情况下,虽然服务仍然会成功绑定到Activity上,但在Activity关闭后,服务虽然会被解除绑定,但并不会被销毁,也就是说,MyService类的onDestroy方法不会被调用。


activity

package com.zeph.android.services;

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;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class ServicesActivity extends Activity {
	private Button myButton;
	private Button myButton2;
	private Button myButton3;
	private Button myButton4;
	private ServiceConnection conn;
	private MyService service;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		conn = new ServiceConnection() {
			// 连接服务失败后,该方法被调用
			@Override
			public void onServiceDisconnected(ComponentName name) {
				System.out.println("ServiceConnection Disconnected");
				Toast.makeText(getApplicationContext(),
						"ServiceConnection Disconnected", Toast.LENGTH_LONG)
						.show();
				service = null;
			}

			// 成功连接服务后,该方法被调用。
			@Override
			public void onServiceConnected(ComponentName name, IBinder ibinder) {
				System.out.println("ServiceConnection Connected");
				Toast.makeText(getApplicationContext(),
						"ServiceConnection Connected", Toast.LENGTH_LONG)
						.show();
				service = ((MyService.MyBinder) ibinder).getMyService();
			}
		};

		myButton = (Button) findViewById(R.id.myButton);
		myButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent();
				intent.setClass(getApplicationContext(), MyService.class);
				startService(intent);
				Toast.makeText(getApplicationContext(), "启动一个Service",
						Toast.LENGTH_LONG).show();
			}
		});
		myButton2 = (Button) findViewById(R.id.myButton2);
		myButton2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(getApplicationContext(), MyService.class);
				stopService(intent);
				Toast.makeText(getApplicationContext(), "关闭一个Service",
						Toast.LENGTH_LONG).show();
			}
		});

		myButton3 = (Button) findViewById(R.id.myButton3);
		myButton3.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent();
				intent.setClass(getApplicationContext(), MyService.class);
				bindService(intent, conn, Context.BIND_AUTO_CREATE);
				Toast.makeText(getApplicationContext(), "绑定一个Service",
						Toast.LENGTH_LONG).show();
				System.out.println("绑定一个Service");
			}
		});
		myButton4 = (Button) findViewById(R.id.myButton4);
		myButton4.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				unbindService(conn);
				Toast.makeText(getApplicationContext(), "取消绑定一个Service",
						Toast.LENGTH_LONG).show();
				System.out.println("取消绑定一个Service");
			}
		});
	}
}
service
package com.zeph.android.services;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

public class MyService extends Service {
	
	private MyBinder binder = new MyBinder();
	
	@Override
	public IBinder onBind(Intent intent) {
		System.out.println("onBind");
		return binder;
	}

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

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

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

	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
		System.out.println("onStart");
	}

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

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

	public class MyBinder extends Binder {
		MyService getMyService() {
			return MyService.this;
		}
	}
}

账号被冻结了,今天才找回来,以前的邮箱不记得了。。。CSDN这次真是遇到了麻烦!!!






你可能感兴趣的:(android,service,null,Class,button)