android 服务
1.新建class 继承android.app.Service;重写其oncreat、onbind、ondestroy、onunbind方法
package com.example.services;
import java.util.Timer;
import java.util.TimerTask;
import android.R.integer;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast;
public class EchoServices extends Service {
@Override
public IBinder onBind(Intent arg0) {
Toast.makeText(this, "启动服务onBind", 1).show();
startTimer();
return bindServices;
}
@Override
public boolean onUnbind(Intent intent) {
Toast.makeText(this, "关闭服务onUnbind", 1).show();
stopTimer();
return super.onUnbind(intent);
}
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "启动服务onCreat", 1).show();
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "关闭服务onDestroy", 1).show();
}
public bindServices bindServices = new bindServices();
public class bindServices extends Binder {
public EchoServices getEchoServices(){
return EchoServices.this;
}
}
private Timer timer=null;
private TimerTask task=null;
private int i=0;
private void startTimer(){
if(timer==null){
timer=new Timer();
task=new TimerTask() {
@Override
public void run() {
i++;
}
};
timer.schedule(task, 1000, 1000);
}
}
private void stopTimer(){
if(timer!=null){
task.cancel();
timer.cancel();
timer=null;
task=null;
}
}
public int getCurrentNumber(){
return i;
}
}
2.清单文件中加入服务组件< service android:name = "EchoServices"></ service >
3.声明意图对象表示服务,开启服务关闭服务
1 package com.example.services;
2
3 import android.os.Bundle;
4 import android.os.IBinder;
5 import android.app.Activity;
6 import android.content.ComponentName;
7 import android.content.Context;
8 import android.content.Intent;
9 import android.content.ServiceConnection;
10 import android.view.Menu;
11 import android.view.View;
12 import android.view.View.OnClickListener;
13 import android.widget.Button;
14 import android.widget.Toast;
15 import android.widget.SearchView.OnCloseListener;
16
17 public class MainActivity extends Activity implements OnClickListener, ServiceConnection {
18
19 private Button btn_start, btn_stop, btn_bind, btn_unbind,btn_getNum;
20 private Intent servicesIntent;
21 private EchoServices echoServices;
22
23 @Override
24 protected void onCreate(Bundle savedInstanceState) {
25 super.onCreate(savedInstanceState);
26 setContentView(R.layout.activity_main);
27 btn_start = (Button) findViewById(R.id.btn_start);
28 btn_stop = (Button) findViewById(R.id.btn_stop);
29 btn_bind = (Button) findViewById(R.id.btn_bind);
30 btn_unbind = (Button) findViewById(R.id.btn_unbind);
31 btn_getNum = (Button) findViewById(R.id.btn_getNum);
32 btn_start.setOnClickListener(this);
33 btn_stop.setOnClickListener(this);
34 btn_bind.setOnClickListener(this);
35 btn_unbind.setOnClickListener(this);
36 btn_getNum.setOnClickListener(this);
37 servicesIntent = new Intent(this, EchoServices.class);
38 }
39
40 @Override
41 public void onClick(View v) {
42 int id = v.getId();
43 switch (id) {
44 case R.id.btn_start:
45 startService(servicesIntent);
46 break;
47 case R.id.btn_stop:
48 stopService(servicesIntent);
49 break;
50 case R.id.btn_bind:
51 bindService(servicesIntent, this, Context.BIND_AUTO_CREATE);
52 break;
53 case R.id.btn_unbind:
54 unbindService(this);
55 break;
56 case R.id.btn_getNum:
57 if(echoServices!=null){
58 System.out.println(echoServices.getCurrentNumber());
59 }
60
61 break;
62 }
63
64 }
65
66 /**
67 * 当服务绑定成功时触发
68 */
69 @Override
70 public void onServiceConnected(ComponentName arg0, IBinder binder) {
71 Toast.makeText(getApplicationContext(), "绑定成功", 1).show();
72 echoServices=((EchoServices.bindServices)binder).getEchoServices();
73 }
74
75 /**
76 * 当绑定失败时触发
77 */
78 @Override
79 public void onServiceDisconnected(ComponentName arg0) {
80 Toast.makeText(getApplicationContext(), "绑定失败", 1).show();
81 }
82
83 }
3 import android.os.Bundle;
4 import android.os.IBinder;
5 import android.app.Activity;
6 import android.content.ComponentName;
7 import android.content.Context;
8 import android.content.Intent;
9 import android.content.ServiceConnection;
10 import android.view.Menu;
11 import android.view.View;
12 import android.view.View.OnClickListener;
13 import android.widget.Button;
14 import android.widget.Toast;
15 import android.widget.SearchView.OnCloseListener;
16
17 public class MainActivity extends Activity implements OnClickListener, ServiceConnection {
18
19 private Button btn_start, btn_stop, btn_bind, btn_unbind,btn_getNum;
20 private Intent servicesIntent;
21 private EchoServices echoServices;
22
23 @Override
24 protected void onCreate(Bundle savedInstanceState) {
25 super.onCreate(savedInstanceState);
26 setContentView(R.layout.activity_main);
27 btn_start = (Button) findViewById(R.id.btn_start);
28 btn_stop = (Button) findViewById(R.id.btn_stop);
29 btn_bind = (Button) findViewById(R.id.btn_bind);
30 btn_unbind = (Button) findViewById(R.id.btn_unbind);
31 btn_getNum = (Button) findViewById(R.id.btn_getNum);
32 btn_start.setOnClickListener(this);
33 btn_stop.setOnClickListener(this);
34 btn_bind.setOnClickListener(this);
35 btn_unbind.setOnClickListener(this);
36 btn_getNum.setOnClickListener(this);
37 servicesIntent = new Intent(this, EchoServices.class);
38 }
39
40 @Override
41 public void onClick(View v) {
42 int id = v.getId();
43 switch (id) {
44 case R.id.btn_start:
45 startService(servicesIntent);
46 break;
47 case R.id.btn_stop:
48 stopService(servicesIntent);
49 break;
50 case R.id.btn_bind:
51 bindService(servicesIntent, this, Context.BIND_AUTO_CREATE);
52 break;
53 case R.id.btn_unbind:
54 unbindService(this);
55 break;
56 case R.id.btn_getNum:
57 if(echoServices!=null){
58 System.out.println(echoServices.getCurrentNumber());
59 }
60
61 break;
62 }
63
64 }
65
66 /**
67 * 当服务绑定成功时触发
68 */
69 @Override
70 public void onServiceConnected(ComponentName arg0, IBinder binder) {
71 Toast.makeText(getApplicationContext(), "绑定成功", 1).show();
72 echoServices=((EchoServices.bindServices)binder).getEchoServices();
73 }
74
75 /**
76 * 当绑定失败时触发
77 */
78 @Override
79 public void onServiceDisconnected(ComponentName arg0) {
80 Toast.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对象里自定义的通信方法。