您的位置:首页 > 移动开发 > Android开发

Android-基础-service

2017-03-15 16:51 295 查看
参考:http://blog.csdn.net/guolin_blog/article/details/11952435

一、基础用法

startService 启动

stopService 停止

bindService(bindIntent, connection, BIND_AUTO_CREATE) 绑定connection

unbindService(connection) 解绑connection

注意:Service运行在主线程中,如果需要做耗时操作,需要新起线程!

二、生命周期

onCreate

service被创建的时候调用。只执行一次。

onStartCommand

每次执行startService的时候都会被调用。

onDestroy

service被销毁

三、activity与service之间的通信

step1:service中定义Binder实例,通过复写onBind方法返回bind实例。

public class MyService extends Service {

private MyBinder mBinder = new MyBinder();

@Override
public void onCreate() {
super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
super.onDestroy();
}

@Override
public IBinder onBind(Intent intent) {
return mBinder;
}

class MyBinder extends Binder {
public void playMusic() {
Log.d("TAG", "playMusic executed");
}

}


}

step2:activity中定义connection实例,在onServiceConnected中获得Service中定义好的Binder实例。

private ServiceConnection playMusicConnection = new ServiceConnection() {

@Override
public void onServiceDisconnected(ComponentName name) {
}

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
myBinder = (MyService.MyBinder) service;
myBinder.playMusic();
}
};


step3:activity中建立绑定关系

Intent bindIntent = new Intent(this, MyService.class);

bindService(bindIntent,playMusicConnection, BIND_AUTO_CREATE);

step4:解除绑定

unbindService(playMusicConnection);

四、start+stop 、bind+unbind必须配套使用

如果Service同时被start和bind,必须在stop和unbind后才会被销毁 这关系到service onDestory方法什么时候被调用

五、远程Service

注册Service的时候将它的android:process属性指定成:remote,该service即具备了远程能力。

远程service已经不和activity在同一个进程,如果当前进程为com.example.test。那么远程service所在的进程一般为com.example.test:remote

既然service已经不在同样的进程中,那么service中定义的binder就无法被activity获取到。

这时候,activity和service之间的就需要用到跨进程通信,即使用AIDL实现IPC。

AIDL(Android Interface Definition Language)是Android接口定义语言的意思,它可以用于让某个Service与多个应用程序组件之间进行跨进程通信,从而可以实现多个应用程序共享同一个Service的功能。

六、 AIDL实现

6.1 新建AIDL文件 MyAIDLService.aidl ,定义接口

interface MyAIDLService {

String toUpperCase(String str); //实现字符串大写转换

}

6.2 Service实现AIDL接口

public class MyService extends Service {

......

@Override
public IBinder onBind(Intent intent) {
return mBinder;
}

MyAIDLService.Stub mBinder = new Stub() {

@Override
public String toUpperCase(String str) throws RemoteException {
if (str != null) {
return str.toUpperCase();
}
return null;
}
};


}

6.3 activity中获取mBinder实例

private MyAIDLService myAIDLService;

private ServiceConnection connection = new ServiceConnection() {

@Override
public void onServiceDisconnected(ComponentName name) {
}

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
myAIDLService = MyAIDLService.Stub.asInterface(service);
try {
String upperStr = myAIDLService.toUpperCase("hello world");
} catch (RemoteException e) {
e.printStackTrace();
}
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android