Service的基本的用法

(1.)这边是Activity对Service操作的类

import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
 * 
 * @项目名:ServiceTest_demo1 
 * @类名称:MainActivity   
 * @类描述:   服务基本用法
 * @创建人:HXF   
 * @修改人:    
 * @创建时间:2015-8-13 上午11:22:54  
 * @version    
 *
 */
public class MainActivity extends Activity implements OnClickListener {

	private Button startService;
	private Button stopService;
	private Button bindService;
	private Button unbindService;
	
	/** 拿到服务的对象 */
	private MyService.DownLoadBinder downLoadBinder;
	
	/** 匿名类,里面的方法在成功绑定的时候和解除绑定的时候调用 */
	private ServiceConnection connection = new ServiceConnection() {
		
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			downLoadBinder = (MyService.DownLoadBinder)service;
			downLoadBinder.startDownload();
			downLoadBinder.getProgress();
		}
		
		@Override
		public void onServiceDisconnected(ComponentName name) {
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		init();
		
	}

	/** 初始化控件 */
	private void init() {
		startService = (Button) findViewById(R.id.start_sercice);
		stopService = (Button) findViewById(R.id.stop_sercice);
		bindService = (Button) findViewById(R.id.bind_sercice);
		unbindService = (Button) findViewById(R.id.UnBind_sercice);
		
		startService.setOnClickListener(this);
		stopService.setOnClickListener(this);
		bindService.setOnClickListener(this);
		unbindService.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.start_sercice:
			/** 开启服务 */
			Intent startIntent = new Intent(this,MyService.class);
			startService(startIntent);
			break;
			
		case R.id.stop_sercice:
			/** 停止服务 */
			Intent stopIntent = new Intent(this,MyService.class);
			stopService(stopIntent);
			
			break;
			
		case R.id.bind_sercice:
			/** 绑定服务 */
			Intent bindIntent = new Intent(this,MyService.class);
			bindService(bindIntent, connection, BIND_AUTO_CREATE);
			
			break;

		case R.id.UnBind_sercice:
			/** 解开绑定服务 */
			unbindService(connection);
			break;


		default:
			break;
		}
	}
}

(2.)继承Service解释一些简单的方法的使用

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
/** 服务的基本用法和方法的解释 */
public class MyService extends Service{

	private DownLoadBinder mBinder = new DownLoadBinder();
	
	@Override
	public IBinder onBind(Intent intent) {
		return mBinder;
	}
	
	/** 会在服务创建的时候调用 */
	@Override
	public void onCreate() {
		super.onCreate();
		Log.d("MyService", "onCreate executed");
	}
	
	/** 会在每次服务启动的时候调用 */
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		Log.d("MyService", "onStartCommand executed");
		return super.onStartCommand(intent, flags, startId);
	}
	
	/** 会在每次服务销毁的时候调用 */
	@Override
	public void onDestroy() {
		super.onDestroy();
		Log.d("MyService", "onDestroy executed");
	}
	
	/**
	 * 
	 * @项目名:ServiceTest_demo1 
	 * @类名称:DownLoadBinder   
	 * @类描述:   绑定服务与activity设置
	 * @创建人:HXF   
	 * @修改人:    
	 * @创建时间:2015-8-13 上午11:52:46  
	 * @version    
	 *
	 */
	class DownLoadBinder extends Binder{
		
		/** 开始下载 */
		public void startDownload(){
			System.out.println("开始下载---startDownload");
		}
		
		/** 查看下载进度 */
		public int getProgress(){
			System.out.println("查看下载进度---getProgress");
			return 0;
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DT从零到壹

您的鼓励是我创作最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值