1.新建class 继承android.app.Service;重写其oncreat、onbind、ondestroy、onunbind方法

packagecom.example.services;


importjava.util.Timer;
importjava.util.TimerTask;

importandroid.R.integer;
importandroid.app.Service;
importandroid.content.Intent;
importandroid.os.Binder;
importandroid.os.IBinder;
importandroid.widget.Toast;

public classEchoServicesextendsService{

@Override
publicIBinderonBind(Intentarg0){
Toast.makeText( this, " 启动服务onBind ", 1).show();
startTimer();
returnbindServices;
}

@Override
publicbooleanonUnbind(Intentintent){
Toast.makeText( this, " 关闭服务onUnbind ", 1).show();
stopTimer();
returnsuper.onUnbind(intent);
}

@Override
public voidonCreate(){
super.onCreate();
Toast.makeText( this, " 启动服务onCreat ", 1).show();
}

@Override
public voidonDestroy(){
super.onDestroy();
Toast.makeText( this, " 关闭服务onDestroy ", 1).show();
}


publicbindServicesbindServices= newbindServices();

public classbindServicesextendsBinder{

publicEchoServicesgetEchoServices(){
returnEchoServices. this;
}
}

privateTimertimer= null;
privateTimerTasktask= null;


private inti= 0;
private voidstartTimer(){
if(timer== null){
timer= newTimer();
task= newTimerTask(){
@Override
public voidrun(){
i++;
}
};
timer.schedule(task, 1000, 1000);
}
}
private voidstopTimer(){
if(timer!= null){
task.cancel();
timer.cancel();
timer= null;
task= null;
}
}

public intgetCurrentNumber(){
returni;
}

}
2.清单文件中加入服务组件 < service android:name = "EchoServices" ></ service >
3.声明意图对象表示服务,开启服务关闭服务

1packagecom.example.services;

2
3 importandroid.os.Bundle;
4 importandroid.os.IBinder;
5 importandroid.app.Activity;
6 importandroid.content.ComponentName;
7 importandroid.content.Context;
8 importandroid.content.Intent;
9 importandroid.content.ServiceConnection;
10 importandroid.view.Menu;
11 importandroid.view.View;
12 importandroid.view.View.OnClickListener;
13 importandroid.widget.Button;
14 importandroid.widget.Toast;
15 importandroid.widget.SearchView.OnCloseListener;
16
17 public classMainActivity extendsActivity implementsOnClickListener,ServiceConnection{
18
19 privateButtonbtn_start,btn_stop,btn_bind,btn_unbind,btn_getNum;
20 privateIntentservicesIntent;
21 privateEchoServicesechoServices;
22
23@Override
24 protected voidonCreate(BundlesavedInstanceState){
25 super.onCreate(savedInstanceState);
26setContentView(R.layout.activity_main);
27btn_start=(Button)findViewById(R.id.btn_start);
28btn_stop=(Button)findViewById(R.id.btn_stop);
29btn_bind=(Button)findViewById(R.id.btn_bind);
30btn_unbind=(Button)findViewById(R.id.btn_unbind);
31btn_getNum=(Button)findViewById(R.id.btn_getNum);
32btn_start.setOnClickListener( this);
33btn_stop.setOnClickListener( this);
34btn_bind.setOnClickListener( this);
35btn_unbind.setOnClickListener( this);
36btn_getNum.setOnClickListener( this);
37servicesIntent= newIntent( this,EchoServices. class);
38}
39
40@Override
41 public voidonClick(Viewv){
42 intid=v.getId();
43 switch(id){
44 caseR.id.btn_start:
45startService(servicesIntent);
46 break;
47 caseR.id.btn_stop:
48stopService(servicesIntent);
49 break;
50 caseR.id.btn_bind:
51bindService(servicesIntent, this,Context.BIND_AUTO_CREATE);
52 break;
53 caseR.id.btn_unbind:
54unbindService( this);
55 break;
56 caseR.id.btn_getNum:
57 if(echoServices!= null){
58System.out.println(echoServices.getCurrentNumber());
59}
60
61 break;
62}
63
64}
65
66 /**
67 *当服务绑定成功时触发
68 */
69@Override
70 public voidonServiceConnected(ComponentNamearg0,IBinderbinder){
71Toast.makeText(getApplicationContext(),"绑定成功",1).show();
72echoServices=((EchoServices.bindServices)binder).getEchoServices();
73}
74
75 /**
76 *当绑定失败时触发
77 */
78@Override
79 public voidonServiceDisconnected(ComponentNamearg0){
80Toast.makeText(getApplicationContext(),"绑定失败",1).show();
81}
82
83}

4.oncreat、onbind、ondestroy、onunbind方法的区别

采用Context.bindService()方法启动服务,在服务未被创建时,系统会先调用服务的onCreate()方法,接着调用onBind()方法,这个时候访问者和服务绑定在一起。如果访问者要与服务进行通信,那么,onBind()方法必须返回Ibinder对象。如果访问者退出了,系统就会先调用服务的onUnbind()方法,接着调用onDestroy()方法。如果调用bindService()方法前服务已经被绑定,多次调用bindService()方法并不会导致多次创建服务及绑定(也就是说onCreate()和onBind()方法并不会被多次调用)。如果访问者希望与正在绑定的服务解除绑定,可以调用unbindService()方法,调用该方法也会导致系统调用服务的onUnbind()-->onDestroy()方法。

Activity与服务进行通信,开发人员通常把通信方法定义在接口里,然后让Ibinder对象实现该接口,而Activity通过该接口引用服务onBind()方法返回的Ibinder对象,然后调用Ibinder对象里自定义的通信方法。

更多相关文章

  1. android定时获取数据更新
  2. android HttpClient网络通信工具类基于XML
  3. Android(安卓)调用系统播放器
  4. Android(安卓)实现模拟按键方法
  5. android点滴3
  6. Android应用程序全屏显示的方法
  7. android的ndk修改app_platform的方法,亲测绝对可行
  8. 浅谈Java中Collections.sort对List排序的两种方法
  9. Python list sort方法的具体使用

随机推荐

  1. Java三大框架SSH面试题锦集
  2. 软件大赛题目----(第一个)Java
  3. 数据截断:不正确的datetime值:“用于行1
  4. 如何在JDBC数据源级别限制从Oracle返回的
  5. JAVA 关于图片上传的代码
  6. JavaEE学习笔记之SSH—Struts2(4)
  7. 关于Java中接口继承接口
  8. Java中怎么把字符串数组转为整形数组
  9. Java并发: CountDownLatch、CyclicBarrier
  10. 有什么方法可以避免HibernateOptimisticL