---
title: "Service1.1"
author: "guzizai2007"
date: "2014-08-29T18:39:18Z"
tags: []
categories: ["非技术"]
slug: "service1-1"
draft: true
lastmod: "2014-08-29T18:39:18Z"
id: 2110910
url: "https://luan.iteye.com/blog/2110910"
---

![](http://www.android-doc.com/images/service_lifecycle.png)

 

定义一个服务：

 

```
package com.example.android_service;

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

/**
 * @desc	自定义Service
 * @author	ljt
 * @time	2014年8月29日 下午5:25:21
 */
public class HelloService extends Service{

	@Override
	public void onCreate() {
		super.onCreate();
		Log.i("HelloService", "HelloService onCreate");
	}
	
	@Override
	public void onDestroy() {
		Log.i("HelloService", "HelloService onDestroy");
		super.onDestroy();
	}
	
	/**
	 * 执行服务的操作
	 * @param intent
	 * @param flags
	 * @param startId
	 * @return
	 */
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		Log.i("HelloService", "HelloService onStartCommand");
		return super.onStartCommand(intent, flags, startId);
	}
	
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}
	
}
```

 启动一个服务：

 

 

```
/**
     * 启动一个服务
     * @param view
     */
    public void startService(View view){
    	Intent intent = new Intent(this,HelloService.class);
    	startService(intent);
    }
```

 停止一个服务：

 

 

```
/**
     * 停止一个服务
     * @param view
     */
    public void stopService(View view){
    	Intent intent = new Intent(this,HelloService.class);
    	stopService(intent);
    }
```

 自身停止服务：

 

 

```
// 自身停止服务
		this.stopSelf();
```

 IntentService：异步服务

 

 

```
package com.example.android_service;

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

/**
 * @desc	异步处理Service
 * @author	ljt
 * @time	2014年8月29日 下午6:05:46
 */
public class WorldService extends IntentService{

	public WorldService() {
		super("WorldService");
	}
	
	/**
	 * 该方法会在一个单独的线程中执行  任务执行完后自动停止
	 */
	@Override
	protected void onHandleIntent(Intent intent) {
		Log.i("WorldService", "WorldService onHandleIntent");
	}
	
	@Override
	public void onDestroy() {
		Log.i("WorldService", "WorldService onDestroy");
		super.onDestroy();
	}
	
	@Override
	public void onCreate() {
		super.onCreate();
		Log.i("WorldService", "WorldService onCreate");
	}
	
}
```

 

```
/**
     * 启动一个IntentService
     * @param view
     */
    public void startIntentService(View view){
    	Intent intent = new Intent(this,WorldService.class);
    	startService(intent);
    }
```

 

```
08-29 18:09:24.340: I/WorldService(7005): WorldService onCreate
08-29 18:09:24.340: I/WorldService(7005): WorldService onHandleIntent
08-29 18:09:24.345: I/WorldService(7005): WorldService onDestroy
```

 可以看到执行完Handler就直接destory了。

 

 

绑定服务：

 

```
package com.example.android_service;

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

/**
 * @desc	绑定服务
 * @author	ljt
 * @time	2014年8月29日 下午6:24:08
 */
public class BindService extends Service{

	@Override
	public IBinder onBind(Intent intent) {
		Log.i("BindService", "BindService onBind");
		return null;
	}
	
	@Override
	public boolean onUnbind(Intent intent) {
		Log.i("BindService", "BindService onUnbind");
		return super.onUnbind(intent);
	}
	
	@Override
	public void onCreate() {
		super.onCreate();
		Log.i("BindService", "BindService onCreate");
	}
	
	@Override
	public void onDestroy() {
		Log.i("BindService", "BindService onDestroy");
		super.onDestroy();
	}
	
}
```

 

```
private ServiceConnection connection = new ServiceConnection() {
		
		@Override
		public void onServiceDisconnected(ComponentName name) {
			Log.i("BindService", "BindService onServiceDisconnected");
		}
		
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			// 绑定对象  service 由BindService onBind返回
			Log.i("BindService", "BindService onServiceConnected");
		}
	};
    
    /**
     * 绑定一个服务
     * @param view
     */
    public void bindService(View view){
    	Intent intent = new Intent(this,BindService.class);
    	// 绑定服务标记  绑定自动创建
    	bindService(intent, connection, BIND_AUTO_CREATE);
    }
    
    /**
     * 解除绑定一个服务
     * @param view
     */
    public void unbindService(View view){
    	unbindService(connection);
    }
```

 

```
08-29 18:35:16.840: I/BindService(8648): BindService onCreate
08-29 18:35:16.870: I/BindService(8648): BindService onBind
08-29 18:35:37.550: I/BindService(8648): BindService onUnbind
08-29 18:35:37.550: I/BindService(8648): BindService onDestroy
```