Service1.1

 

定义一个服务:

 

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

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值