Service1

简介:

Service是Android四大组件之一,运行在后台执行耗时操作并且不提供用户界面。其他组件(如Activity)可以通过startService启动该组件,也可以通过BindService启动并绑定该组件进行通信。

使用场景:后台下载文件,播放音乐等。

注意:Service运行在主线程中,他不会创建属于自己的线程,也不是运行在独立的线程中,所以,在使用的时候,需要自己创建线程,而不应该直接执行耗时操作,这样会引起ANR。

StartService

其他组件如Activity通过调用StartService启动该Service。拥有独立的生命周期,不依赖启动它的组件。

package com.example.serviceone;

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

public class MainActivity extends Activity {

	
	private Button button;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		button = (Button)findViewById(R.id.button1);
		button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(MainActivity.this,MyServiceOne.class);
				//startService方式启动的service有自己的生命周期,不依赖启动他的组件
				startService(intent);
			}
		});
	}

}

package com.example.serviceone;

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

public class MyServiceOne extends Service{

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		
		
		//Service的操作在主线程里进行,会导致程序ANR
//		try {
//			
//			for(int i = 1 ; i<=1000; i++)
//			{
//				System.out.println("当前的值是: " + i);
//				
//				Thread.sleep(1000);
//			}
//			
//		} catch (InterruptedException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		
		//在主线程中调用自己创建的线程
		ThreadOne thread = new ThreadOne();
		thread.start();
	}
	
	//为了防止在主线程中操作ANR的问题,需要自己创建一个线程
	public class ThreadOne extends Thread
	{
		@Override
		public void run() {
			// TODO Auto-generated method stub
			super.run();
			
			try {
				
				for(int i = 1 ; i<=1000; i++)
				{
					System.out.println("当前的值是: " + i);
					
					Thread.sleep(1000);
				}
				
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		return super.onStartCommand(intent, flags, startId);
	}
	

}


BoundService

其他组件未了与Service简历一个长时间的链接,通过调用bindService启动并绑定该Servcie。并能与之交互(发送请求,接受相应)。生命周期依赖绑定它的组件,一旦所有绑定它的组件取消绑定,则消亡。

package com.example.serviceone;

import java.util.Random;

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

public class BoundServiceOne extends Service {

	//第三步,在onBind中返回一个BinderService实例
	BinderService binderService = new BinderService();
	
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return binderService;
	}

	//第一步,在Service中创建一个Binder子类
	public class BinderService extends Binder
	{
		//第二步,在该子类中创建一个方法,返回当前Service实例
		public BoundServiceOne getService()
		{
			return BoundServiceOne.this;
		}
	}
	
	//第四步,在Service中创建业务方法,必须是public
	public int getRandom()
	{
		Random random = new Random();
		return random.nextInt(100);
	}
	
	//解除绑定service时要调用此方法
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		System.out.println("Service已经解除绑定");
	}
}

package com.example.serviceone;

import com.example.serviceone.BoundServiceOne.BinderService;

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 BindServiceActivity extends Activity {

	private Button button;
	private BoundServiceOne myService;
	private Boolean isBound;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.boundservicelayout);
		button = (Button)findViewById(R.id.button1);
		button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//绑定成功后,可以调用Service中的方法
				Toast.makeText(myService, myService.getRandom()+"", 1000).show();
			}
		});
	}
	
	//在onstart方法中绑定service
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		//BoundService也是通过intent启动的
		Intent intent = new Intent(this,BoundServiceOne.class);
		//开始绑定service
		bindService(intent, connection, Context.BIND_AUTO_CREATE);
		
	}
	
	ServiceConnection connection = new ServiceConnection() {
		
		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub
			isBound = false;
		}
		
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			// TODO Auto-generated method stub
			//参数service是一个IBinder类型的,强制转换为BinderService类型的对象
			BinderService binderService = (BinderService)service;
			//得到BoundService
			myService = binderService.getService();
			isBound = true;
		}
	};
	
	//OnStop方法中解除绑定
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		unbindService(connection);
	}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值