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 实现模拟按键方法
  3. Android屏幕横屏竖屏切换的方法
  4. Android WIFI热点默认SSID的修改方法
  5. Android:软件卸载的另一种方法adb uninstall
  6. android 网络判断的几种方法
  7. Android ListView滑动时出现黑屏解决方法

随机推荐

  1. Android(安卓)Property Animation 介绍(一
  2. Android隐藏标题栏的方法
  3. Android(安卓)Handler机制总结
  4. Android(安卓)中短信数据库的简单操作
  5. 1.4.1 Android的编码规范
  6. android 背光驱动
  7. 一个很酷的加载loading效果
  8. Android(安卓)OpenSSL分析及实例
  9. Android(安卓)studio 升级Android(安卓)S
  10. 获取Android设备的方向 -- gsensor