Service 是安卓的四大组件之一,使用的时候需要在清单文件中进行注册。
注意:
1. 在清单文件中注册,一般只需引入name 属性;
2. 服务不能自己运行,需要通过调用启动方法,启动服务。 一般为活动Activity中启动服务。
应用场景
1.1 执行需要长时间运行的操作,这个操作不与用户进行交互,如网络下载、大文件I/O 、复杂计算等。
1.2 两个应用间动态交互一般通过Server来完成。
应用内或应用间数据通信,Android每个应用程序都在自己的dalvik虚拟机中运行。
一个应用是不允许访问其他应用的内存信息的,为此Android 引入了 Content Provider在不同的应用间共享数据,
BroadcastReceiver 广播信息给不同的应用程序,但 Content Provide 更多用于数据的共享,BroadcastReceiver
广播信息会被所有应用接收较耗费系统的资源,对于两个应用间动态的进行交互还需要通过Service 来完成。Service 的实现方式:
继承Service 类public class MyServer extends Service
重写绑定方法
public IBinder (Intent intent){ rerurn null;}
启动 Service 有两种方式
第一种:通过startServer() 启动Service
生命周期:
onCreate()- >onStart()(可多次调用)–服务运行->onDestroy()如果Service还没有运行,则android先调用onCreate()
然后调用onStart();如果Service已经运行,则只调用onStart(),
所以一个Service的onStart方法可能会重复调用多次。开启者调用stopService的时候直接onDestroy,
如果是开启者自己直接退出而没有调用stopService的话,Service会一直在后台运行。
该Service的开启者再启动起来后可以通过stopService关闭Service。第二种:通过bindService() 启动Service 该方式提供了Context,例如活动等组件调用Sercive内部方法的机制。
生命周期:
onCreate()- >onBind()(只一次,不可多次绑定) -> onUnbind->onDestroy()onBind方法将返回给客户端一个IBind接口实例,IBind允许客户端回调服务的方法,比如得到Service运行
的状态或其他操作。Service 常用的方法 ,可以重写的方法
//绑定的方法
public IBinder onBind(Intent intent) {}
//创建的时候调用
public void onCreate() {super.onCreate();}
//服务开启调用的方法,已过时,现已使用onStartCommand 来处理数据。
public void onStart(Intent intent, int startId) {super.onStart(intent, startId);}
//创建后调用该方法 该方法会处理一些数据信息(参数为一些数据)
public int onStartCommand(Intent intent, int flags, int startId) {return super.onStartCommand(intent, flags, startId);}
//服务销毁的方法
public void onDestroy() {super.onDestroy();}Context调用服务使用的方法:
例如:活动中调用
启动服务:
1. // 开启服务
startService(intent);
Intent intent = new Intent(MainActivity.this, MyService.class);
2.// 绑定服务
bindService(intent, new MyConn(), BIND_AUTO_CREATE);// service intent,conn,ServiceConnection连接,flags,标示
Intent intent = new Intent(MainActivity.this, MyService.class);class MyConn implements ServiceConnection { // onServiceConnected 当服务连接的时候 //IBinder 服务返回的IBinder对象 public void onServiceConnected(ComponentName name, IBinder service) { } // onServiceDisconnected 当服务断开的时候调用 public void onServiceDisconnected(ComponentName name) { } } 3.stopService(intent); 4.unbindService(conn);
常见的应用情形: Service的信息交互
//绑定者
bindService(Intent service, ServiceConnection conn, int flags);//绑定服务
//参数一:通过intent意图,指定要绑定的服务
//参数二:服务连接状态的接口 //参数三:绑定server方式的表示实现ServiceConnection 接口的两个方法:
public void onServiceConnected(ComponentName name, IBinder service) {}
//参数一:已连接的服务的具体组件名称
//参数二:onBind方法返回的Binder对象public void onServiceDisconnected(ComponentName name) {}
//参数一:失去连接的服务的具体组件名称//服务本身
通过onBind方法的返回值向绑定者返回一个Binder对象(相当于中间人角色),从而去调用服务的方法5.1 Activity 中接收处理服务 Service 中的信息 通过 bindService() 活动来实现。
主要代码实现方式:
方式一:安全性好的 ,使用接口回调: 该方法只提供接口中的方法,相当于通过接口进行过滤,筛选中符合的接口中的方法,较为安全。
在活动中:
public class MainActivity extends Activity implements OnClickListener {
private ServerConnect sc;
private Intent intent;
// 1.开启服务的两种模式 startServer 和BindServer
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化控件
Button bt_getserver = (Button) findViewById(R.id.bt_getserver);
Button bt_bind = (Button) findViewById(R.id.bt_bind);
// 初始化事件
bt_getserver.setOnClickListener(this);
bt_bind.setOnClickListener(this);
}@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_getserver:
// 通过中间人调用
sc.getOneNet();
break;case R.id.bt_bind: // 获取意图对象,进行数据传递 intent = new Intent(getApplicationContext(), MyService.class); // 2 绑定服务 tag 绑定自动创建 bindService(intent, new MyConn(), BIND_AUTO_CREATE); break; default: break; }
}
class MyConn implements ServiceConnection {
// 在服务连接的时候调用 service 为服务器传入的内容
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
sc = (ServerConnect) service;
}// 在服务销毁的时候调用 @Override public void onServiceDisconnected(ComponentName name) { }
}
}在服务中:
public class MyService extends Service {
private String tag=”MyService”;public IBinder onBind(Intent intent) { return new MyBinder();}
private void getNet() { Toast.makeText(getApplicationContext(), “获取网络资源。。。”, 0).show();}
class MyBinder extends Binder implements ServerConnect { public void getOneNet() {getNet();} }
}接口中:
public interface ServerConnect { void getOneNet();}在xml文件中: