Android-基础-service
来源:互联网 发布:广州多益网络官网充值 编辑:程序博客网 时间:2024/06/18 10:11
参考: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();@Overridepublic void onCreate() { super.onCreate();}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) { return super.onStartCommand(intent, flags, startId);}@Overridepublic void onDestroy() { super.onDestroy();}@Overridepublic 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 {
......@Overridepublic 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基础 : Android Service
- Android基础 : Android Service
- Android基础之Service
- Android (一) Service基础
- Android基础之Service
- 【Android】Service基础学习
- android service基础
- Android基础------Service服务
- Android--Service之基础
- Android--Service之基础
- android Service基础
- Android--Service之基础
- Android基础--Service应用
- Android基础service简介
- [Android基础]Service
- android的service基础
- Android Service基础
- Android Service 基础详解
- 秒杀系统的分析
- Hadoop和HBase安装设置入门篇
- Z-cluster tolerance and ranks
- 从第一字符串中删除第二个字符串中所有的字符
- mysql max_allowed_packet 设置过小导致记录写入失败
- Android-基础-service
- JavaWeb之文件上传与下载详解
- Scala的安装
- LeetCode105--Tree--Construct Binary Tree from Preorder and Inorder Traversal--根据前序遍历+中序遍历构建树
- Android RecyclerView之GridView显示(三)
- Controller的处理
- Python selenium —— 一定要会用selenium的等待,三种等待方式解
- linux系统运维10
- 直接内存访问(DMA)