-
Service的基本概念
Service是Android系统中的四大组件之一,它跟Activity的级别差不多,但不能自己运行,只能后台运行,并且可以和其他组件进行交互。service可以在很多场合的应用中使用,比如播放多媒体的时候用户启动了其他Activity,这个时候程序就要在后台继续播放,比如检测SD卡上文件的变化,再或者后台记录你地理信息位置的改变等等,总之服务总是藏在后台的。 -
Service的生命周期
启动一个服务
public void startClick(View v){
Intent intent=new Intent(this,MyService.class);
startService(intent);
}
停止一个服务
public void stopClick(View v){
Intent intent=new Intent(this,MyService.class);
stopService(intent);
}
MyService代码
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class MyService extends Service {
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
super.onCreate();
System.out.println("my Service create");
}
//在该方法中实现服务的核心业务
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
for (int i=0;i<50;i++){
System.out.println("onStartCommand"+i);
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
System.out.println("my service Destroy");
}
}
效果如下
点击启动一个服务
再次点击
不会重新创建一个服务,而是直接调用onStartCommand方法
直接点击手机的返回键,服务不会被关闭,只有点击关闭一个服务按钮,服务才会被关闭
或者调用stopSelf来终止服务;
public int onStartCommand(Intent intent, int flags, int startId) {
for (int i=0;i<50;i++){
System.out.println("onStartCommand"+i);
if(i==30){
this.stopSelf();//终止服务
break;
}
}
return super.onStartCommand(intent, flags, startId);
}
默认情况下服务与主线程在同一个线程中执行,如果服务执行一个比较耗时的操作,我们必须使用子线程来完成工作避免阻塞主线程
修改代码如下
public int onStartCommand(Intent intent, int flags, int startId) {
//使用线程完成长时间的工作
new Thread(new Runnable() {
@Override
public void run() {
for (int i=0;i<50;i++){
System.out.println("onStartCommand-"+i+"-"+Thread.currentThread().getName());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(i==30){
MyService.this.stopSelf();//终止服务
break;
}
}
}
}).start();
return super.onStartCommand(intent, flags, startId);
}
onStartComand使用时,返回的是一个(int)整形
这个整形可以有四个返回值:start_sticky、start_no_sticky、START_REDELIVER_INTENT、START_STICKY_COMPATIBILITY。
1):START_STICKY:如果service进程被kill掉,保留service的状态为开始状态,但不保留递送的intent对象。随后系统会尝试重新创建service,由于服务状态为开始状态,所以创建服务后一定会调用onStartCommand(Intent,int,int)方法。如果在此期间没有任何启动命令被传递到service,那么参数Intent将为null。
2):START_NOT_STICKY:“非粘性的”。使用这个返回值时,如果在执行完onStartCommand后,服务被异常kill掉,系统不会自动重启该服务。
3):START_REDELIVER_INTENT:重传Intent。使用这个返回值时,如果在执行完onStartCommand后,服务被异常kill掉,系统会自动重启该服务,并将Intent的值传入。
4):START_STICKY_COMPATIBILITY:START_STICKY的兼容版本,但不保证服务被kill后一定能重启。
- IntentService的使用
1、内部有一个工作线程来完成耗时的操作,只需实现onHandleIntent方法即可
2、完成工作后会自动停止服务
3、同时执行多个任务时,会以工作队列的方式,依次执行
4、通常使用该类来完成本APP中的耗时工作
public class MyIntentService extends IntentService {
public MyIntentService() {
super("MyIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
System.out.println(intent.getStringExtra("info"));
for(int i=0;i<50;i++){
System.out.println("onHandleIntent-"+i+"-"+Thread.currentThread().getName());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
启动IntentService
public void startIntentServiceClick(View v){
Intent intent=new Intent(this,MyIntentService.class);
intent.putExtra("info","顶不住了");
startService(intent);
}
清单文件自动生成
- Bind Service
应用程序组件(客户端)通过调用bindService()方法能够绑定服务,然后Android系统会调用服务的onBind()回调方法,这个方法会返回一个跟服务端交互的IBinder对象。这个绑定是异步的,bindService()方法立即返回,并且不给客户端返回IBinder对象。要接收IBinder对象,客户端必须创建一个ServiceConnection类的实例,并且把这个实例传递给bindService()方法。ServiceConnection对象包含了一个系统调用的传递IBinder对象的回调方法。
注意:只有Activity,service,和内容提供器(content provider)能够绑定服务—对于广播接收器不能绑定服务。
要实现绑定服务,要先创建一个AIDL文件
// ICat.aidl
package com.example.service;
// Declare any non-default types here with import statements
interface ICat {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
double aDouble, String aString);
void setName(String name);
String desc();
}
之后创建一个具体的接口实现类
import android.os.RemoteException;
public class CatImpl extends ICat.Stub {
private String name;
public void setName(String name) throws RemoteException{
this.name=name;
}
public String desc() throws RemoteException{
return "hello my name is "+name+", I am a Cat";
}
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
}
}
新建一个BoundService
public class MyBoundService extends Service {
public MyBoundService() {
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public IBinder onBind(Intent intent) {
return new CatImpl();
}
@Override
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
实现方法
绑定一个服务
public void boundClick(View v){
Intent intent=new Intent(this,MyBoundService.class);
//异步绑定,绑定成功后会回调onServiceConnected
bindService(intent,conn, Context.BIND_AUTO_CREATE);
}
解除绑定
public void unboundClick(View v) {
if (mBound) {
unbindService(conn);
Toast.makeText(MainActivity.this,"解除绑定成功",Toast.LENGTH_SHORT).show();
}
}
绑定服务的连接回调接口
private ServiceConnection conn=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//绑定成功后回调的方法
cat=ICat.Stub.asInterface(service);
mBound=true;
Toast.makeText(MainActivity.this,"绑定成功",Toast.LENGTH_SHORT).show();
}
@Override
public void onServiceDisconnected(ComponentName name) {
//服务异常时调用
mBound=false;
}
};
通过IPC调用业务方法
public void callClick(View v){
if(cat==null){return;}
try {
cat.setName("喵喵");
Toast.makeText(this,cat.desc(),Toast.LENGTH_SHORT).show();
} catch (RemoteException e) {
e.printStackTrace();
}
}
绑定服务:
- 通过绑定服务来实现功能的步骤
- 1、客户端通过bindService方法来绑定一个服务对象,如果绑定成功,会回调用ServiceConnection接口方法onServiceconnected
- 2、通过Service组件来暴露业务接口
- 3、服务端通过创建一个*.aidl文件来定义一个可以被客户端调用的业务接口
- 一个aidl文件:
- (1)不能有修饰符,类似接口的写法
- (2)支持类型:8种基本数据类型,String,CharSequence,List,Map,自定义类型
-
自定义类型:
-
实现Parcelable接口
-
定义一个aidl文件声明该类型:parcelable Person;
-
在其它aidl文件中使用,必须要使用import
- 4、服务端需要提供一个业务接口的实现类,通过我们会extends Stub类
- 5、通过Service的onBind方法返回被绑定的业务对象
- 6、客户端如果绑定成功,就可以像调用自己的方法一样调用远程的业务对象方法
- 使用技巧
- started启动的服务会长期存在,只要不停
- bind启动的服务通常会在解绑时停止
- 先started,后bind