android_Service与Binder的关系





  Service与Binder的关系:

  客户端的服务无法注册为系统服务;客户端服务只能为该客户端服务;

  系统定制,将自定义服务注册到SystemServer,需在开机过程中添加;位置frameworks/base/services/java/com/android/server/下SystemServer.java;
  run方法中,ServiceManager.addService(Context.My_Service,new MyToolService(context));

  Binder驱动代码运行在内核态,客户端通过系统调用完成的。
  仅基于Binder类编写服务,但只是一部分。系统服务指可以使用getSystemService()方法获取的服务。
  客户端的服务则必须基于Service类来编写。某一应用客户端服务不能为其他客户端应用提供服务(个人意见)。
  android.app包,public abstract class Service extends ContextWrapper implements ComponentCallbacks2{ *** }
    public class Application extends ContextWrapper implements ComponentCallbacks2{ *** }
    public final class ActivityThread{ *** }
  android.os包,public class Binder implements IBinder{ *** }
  android.view包,public final class ViewRootImpl implements ViewParent,View.AttachInfo.Callbacks,HardwareRenderer.HardwareDrawCallbacks { *** }
    public interface ViewParent{ *** }
  com.android.server.am包,public final class ActivityManagerService extends ActivityManagerNative implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback{ *** }
    com.android.server包,public class Watchdog extends Thread{ *** }
    com.android.internal.os包,public final class BatteryStatsImpl extends BatteryStats{ *** }
  android.os包,public interface IInterface{  public IBinder asBinder(); }  #Base class for Binder interfaces.  When defining a new interface,you must derive it from IInterface.
    public interface IBinder{ *** }  #Base interface for a remotable object, the core part of a lightweight remote procedure call mechanism designed for high performance when performing in-process and cross-process calls.
  android.content包,public interface ServiceConnection{ *** }  #mContext.bindService(**),需要传入ServiceConnection接口。


  Service分类:LocalService;RemoteService;
  Messenger:Reference to a Handler, which others can use to send messages to it. This allows for the implementation of message-based communication across processes, by creating a Messenger pointing to a Handler in one process, and handing that Messenger to another process.
  android:isolatedProcess="true" //If set to true, this service will run under a special process that is isolated from the rest of the system and has no permissions of its own. The only communication with it is through the Service API (binding and starting).
  android:exported="false" //是否允许activity/Service能够跨进程调用;default为true;
  android:permission="***" //其他应用必须也设置usepermission xxx.xxx.xx才能调用该activity/Service;
  android:process=":remote" //代表新建进程;
  android:enabled=""; //Whether or not the service can be instantiated by the system;
  android:process=""; //The name of the process where the service is to run. Normally, all components of an application run in the default process created for the application.




发布了8 篇原创文章 · 获赞 7 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/liu31187/article/details/22871089