
<span style="font-family:Times New Roman;font-size:18px;">package com.example.servicetest1;
import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
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 MainActivity extends Activity {
private Button startService;
private Button stopService;
private Button bindService;
private Button unbindService;
private Button getData;
private MyBindService.MyIBinder binder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//a.获取界面组件
startService = (Button)findViewById(R.id.start);
stopService = (Button)findViewById(R.id.stop);
bindService = (Button)findViewById(R.id.bind);
unbindService = (Button)findViewById(R.id.unbind);
getData = (Button)findViewById(R.id.get);
//b.为组件注册同一个监听器
MyEvent Listener = new MyEvent(); //实例化一个事件监听器
startService.setOnClickListener(Listener); //为组件注册监听器对象
stopService.setOnClickListener(Listener);
bindService.setOnClickListener(Listener);
unbindService.setOnClickListener(Listener);
getData.setOnClickListener(Listener);
}
/*1.事件处理内部类
* 通过获取视图组件(v)并判定组件ID,来响应具体组件*/
public class MyEvent implements OnClickListener
{
//a.设置启动指定Service的"意图"
Intent intent1 = new Intent("com.jiangdongguo.MyService");
Intent intent2 = new Intent("com.jiangdongguo.MyBindService");
//b.启动指定的service
public void onClick(View v)
{
switch (v.getId())
{
//(1)启动普通Service
case R.id.start:
startService(intent1); //启动"intent"指定的Service服务
break;
//(2)关闭普通Service
case R.id.stop:
stopService(intent1); //关闭"intent"指定的Service服务
break;
//(3)启动并绑定Service
case R.id.bind:
bindService(intent2, conn, Service.BIND_AUTO_CREATE);
break;
//(4)解除与Service绑定
case R.id.unbind:
unbindService(conn);
break;
//(5)从service服务获取数据
case R.id.get:
Toast.makeText(MainActivity.this,"从Service读取到的数据为:count="+Integer.toString(binder.getCount()), Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
/*2.ServcieConnection匿名内部类:创建一个ServcieConnection对象
* 该内部类的对象用于监听访问者与Service之间的连接情况
* (1)连接成功:回调该ServiceConnection对象的onServiceConnected(ComponentName name,IBinder service)方法
* (2)连接失败:回调其onServiceDisconnected(ComponentName name)方法*/
private ServiceConnection conn = new ServiceConnection()
{
//a.连接服务成功:该方法用于获取Service返回的IBinder对象,用于与Service进行通信
public void onServiceConnected(ComponentName name, IBinder service)
{
System.out.println("---MainActivity's onServiceConnected invoked!---");
binder = (MyBindService.MyIBinder)service; //获取Service返回的IBinder对象
}
//b.连接服务失败:提示失败
public void onServiceDisconnected(ComponentName name) {
System.out.println("---MainActivity's onServiceDisconnected invoked!---");
Toast.makeText(MainActivity.this, "与Service绑定失败,请重试。", Toast.LENGTH_SHORT).show();
}
};
}</span><span style="font-family:Times New Roman;font-size:18px;">package com.example.servicetest1;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class MyService extends Service {
/*1.onBind方法
* service用于返回一个IBinder对象给客户端方便通信
* 如果是访问者通过Context.startService启动该Service,则返回为空
*/
@Override
public IBinder onBind(Intent arg0) {
System.out.println("---MyService’s onBind invoked!---");
return null;
}
/*2.onCreate方法
* 当Service启动后,自动调用该方法,用于初始化
* */
public void onCreate() {
System.out.println("---MyService’s onCreate invoked!---");
super.onCreate();
}
/*3.onStartCommand方法
* 每次访问者调用startService方法启动该Service时都会回调该方法*/
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("---MyService’s onStartCommand invoked!---");
return super.onStartCommand(intent, flags, startId);
}
/*4.onDestroy方法
* 当访问者调用Context.stopService方法后,调用该方法关闭Service服务
* */
public void onDestroy() {
System.out.println("---MyService’s onDestory invoked!---");
super.onDestroy();
}
}</span><span style="font-family:Times New Roman;font-size:18px;">package com.example.servicetest1;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class MyBindService extends Service {
private int count=0;
private boolean threadFlag=true; //结束线程标志
MyIBinder bind = new MyIBinder(); //实例化一个Binder对象,用于被onBind方法返回给访问者
/*0.Binder子类
* 其对象用于与访问者通信,访问者通过调用IBinder子类中的方法获取Service中的数据*/
public class MyIBinder extends Binder
{
//自定义成员方法,用于返回Service中的数据
public int getCount()
{
return count;
}
}
/*1.onBind方法
* service用于返回一个IBinder对象给客户端方便通信
* 如果是访问者通过Context.startService启动该Service,则返回为空
*/
@Override
public IBinder onBind(Intent arg0) {
System.out.println("---MyService’s onBind invoked!---");
return bind;
}
/*2.onCreate方法
* 当Service启动后,自动调用该方法,用于初始化
* */
public void onCreate() {
System.out.println("---MyService’s onCreate invoked!---");
//创建一个线程,用于按一定时间间隔更新count的值
new Thread(){
public void run()
{
while(threadFlag) //默认为true
{
try
{
Thread.sleep(500);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
count++;
}
}
}.start();
super.onCreate();
}
/*3.onStartCommand方法
* 每次访问者调用startService方法启动该Service时都会回调该方法*/
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("---MyService’s onStartCommand invoked!---");
return super.onStartCommand(intent, flags, startId);
}
/*4.onDestroy方法
* 当访问者调用Context.stopService方法后,调用该方法关闭Service服务
* */
public void onDestroy() {
System.out.println("---MyService’s onDestory invoked!---");
threadFlag=false;
super.onDestroy();
}
/*5.onUnbind方法
* 当访问者调调用Context.unBind()方法后,调用该方法与Service解除绑定*/
public boolean onUnbind(Intent intent) {
System.out.println("---MyService’s onUnbind invoked!---");
return super.onUnbind(intent);
}
}</span>

原文:http://blog.csdn.net/u012637501/article/details/44537955