AIDL和Service的区别是什么

参考:


简单来说,local_service就是指处于当前进程中的service,remote service就是不同进程中的service。

这两者的区别在于,local_servie由于是同一进程的不同线程中,因此不需要编写aidl文件来实现ipc机制,也不需要通过ipc机制来bind,服务端只要new一个Binder出来就行了。
类似这样
Public  class ServiceBinder extends Binder implements IService;
...
ServiceBinder serviceBinder = new ServiceBinder();

remote service由于不在同一进程中,因此需要编写aidl文件来实现ipc通讯。
此外服务端的binder对象是通过stub来取得的。
IService.Stub serviceBinder = new ICountService.Stub() 

访问这两种service的方法一样,只不过访问remote servie的时候需要捕获异常。
再问两者适用场合? 
local service 和另起线程有何区别?
remote service由于不在同一进程中,因此需要编写aidl文件来实现ipc通讯。
有何道理?
有何道理?
为了不用我们自己去编写stub等一套繁琐的ipc机制的实现,android提供了aidl这个东西,你只要编写aidl文件,编译系统就会为你自动生成实现ipc需要的代码。

使用场合如上所说,如果你只是想做个给自己用的service,那你就用local service,如果你弄一个系统中共用的service,也就是系统中所有的程序动能访问的service,那你就要用remote service。
我的意思是,如果只是单向的让service 完成一些同一类型异步的任务,是否一定要写aidl来完成呢?
local service 在主线程执行,完成费时任务自然要另起线程。为何不直接另起线程来完成呢?
需不需要写aidl与你想让service干什么无关,只要是remote service,就需要写。

local service 与线程的概念不再一个层面,定位也不同。你用local service去实现的功能有些当然是另起thread也能实现。实现的方法不同,自然也各有优劣。这个由线程和service本身的特性决定。比如service仅存在一个实例。
local service没有ui,因此只有当当前进程的所有线程都结束了,只剩下local service所在的线程的情况下,local service在主线程执行这句话才有意义。
完成费时任务可以另起线程,也可以另起进程,或者启动一个local or remote service,没有规定费时任务一定要另起线程。
sdk doc 中一段话 说明使用service 和使用分线程的区别。
Because a process running a service is ranked higher than one with background activities, an activity that initiates a long-running operation might do well to start a service for that operation, rather than simply spawn a thread — particularly if the operation will likely outlast the activity. Examples of this are playing music in the background and uploading a picture taken by the camera to a web site. Using a service guarantees that the operation will have at least "service process" priority, regardless of what happens to the activity. As noted in the Broadcast receiver lifecycle section earlier, this is the same reason that broadcast receivers should employ services rather than simply put time-consuming operations in a thread.
Because a process running a service is ranked higher than one with background activities, an activity that initiates a long-running operation might do well to start a service for that operation, rather than simply spawn a thread — particularly if the operation will likely outlast the activity. Examples of this are playing music in the background and uploading a picture taken by the camera to a web site. Using a service guarantees that the operation will have at least "service process" priority, regardless of what happens to the activity. As noted in the Broadcast receiver lifecycle section earlier, this is the same reason that broadcast receivers should employ services rather than simply put time-consuming operations in a thread.
只有service 的进程比只有分线程的process 优先级 高  持续时间有可能更长。
我现在也很想问这个问题,我的local service也可以被其他application以startService(intent)的方式调用,或者写aidl,被别的application以bindService的方式调用,为什么要另开一个remote process呢,是不是和应用资源或者performance有关系呢?
Meet so Meet. C plusplus I-PLUS....
原文地址:https://www.cnblogs.com/iplus/p/4467418.html