1.远程服务简介

  • 什么是远程服务
    远程服务(Remote Service)也被称之为独立进程,它不受其它进程影响,可以为其它应用程序提供调用的接口——实际上就是进程间通信IPC(Inter-Process Communication),Android提供了AIDL(Android Interface Definition Language,接口描述语言)工具来帮助进程间接口的建立。

在Android中,不同的应用属于不同的进程(Process),一个进程不能访问其它进程的存储(可以通过ContentProvider实现,如:通讯录的读取)。

  • 远程服务的适用场景
    一般适用于为其它应用程序提供公共服务的Service,这种Service即为系统常驻的Service(如:天气服务等)。
  • 远程服务的优缺点
  • 优点
    1.远程服务有自己的独立进程,不会受到其它进程的影响;
    2.可以被其它进程复用,提供公共服务;
    3.具有很高的灵活性。
  • 缺点
    相对普通服务,占用系统资源较多,使用AIDL进行IPC也相对麻烦。

2.远程服务的创建

  • 定义AIDL接口
    通过AIDL文件定义服务(Service)向客户端(Client)提供的接口,我们需要在对应的目录下添加一个后缀为.aidl的文件(注意,不是.java),IMyAidlInterface.aidl文件内容如下:
package com.zihao.remoteservice.server;interface IMyAidlInterface {  String getMessage();}

注:如果服务端与客户端不在同一App上,需要在客户端、服务端两侧都建立该aidl文件。

  • 新建Remote Service
    在远程服务中,通过Service的onBind(),在客户端与服务端建立连接时,用来传递Stub(存根)对象。
// 远程服务示例public class RemoteService extends Service {    public RemoteService() {  }    @Override  public IBinder onBind(Intent intent) {    return stub;// 在客户端连接服务端时,Stub通过ServiceConnection传递到客户端  }    // 实现接口中暴露给客户端的Stub--Stub继承自Binder,它实现了IBinder接口  private IMyAidlInterface.Stub stub = new IMyAidlInterface.Stub(){      // 实现了AIDL文件中定义的方法    @Override    public String getMessage() throws RemoteException {      // 在这里我们只是用来模拟调用效果,因此随便反馈值给客户端      return "Remote Service方法调用成功";            }      };  }

同时,在AndroidManifest.xml中对Remote Service进行如下配置:

        

如果客户端与服务端在同个App中,AndroidManifest.xml中设置Remote Service的andorid:process属性时,如果被设置的进程名是以一个冒号(:)开头的,则这个新的进程对于这个应用来说是私有的,当它被需要或者这个服务需要在新进程中运行的时候,这个新进程将会被创建。如果这个进程的名字是以小写字符开头的,则这个服务将运行在一个以这个名字命名的全局的进程中,当然前提是它有相应的权限。这将允许在不同应用中的各种组件可以共享一个进程,从而减少资源的占用。


3.客户端调用远程服务接口

在客户端中建立与Remote Service的连接,获取Stub,然后调用Remote Service提供的方法来获取对应数据。

public class MainActivity extends AppCompatActivity {    private IMyAidlInterface iMyAidlInterface;// 定义接口变量  private ServiceConnection connection;    @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);     setContentView(R.layout.activity_main);    bindRemoteService();  }    private void bindRemoteService() {    Intent intentService = new Intent();    intentService.setClassName(this,"com.zihao.remoteservice.RemoteService");      connection = new ServiceConnection() {      @Override      public void onServiceConnected(ComponentName componentName,IBinder iBinder) {        // 从连接中获取Stub对象        iMyAidlInterface = IMyAidlInterface.Stub.asInterface(iBinder);        // 调用Remote Service提供的方法        try {          Log.d("MainActivity", "获取到消息:" + iMyAidlInterface.getMessage());         } catch (RemoteException e) {          e.printStackTrace();        }       }         @Override      public void onServiceDisconnected(ComponentName componentName) {        // 断开连接        iMyAidlInterface = null;      }    };       bindService(intentService, connection, Context.BIND_AUTO_CREATE);  }     @Override  protected void onDestroy() {    super.onDestroy();     if (connection != null)      unbindService(connection);// 解除绑定  }}
log.png 【Android】远程服务(Remote Service)的使用_第1张图片 通信示意图.png

4.拓展阅读

Android:关于声明文件中android:process属性说明
【Android】Service那点事儿
【Android】Service前台服务的使用

更多相关文章

  1. 淘宝(阿里百川)手机客户端开发日记第二篇 android首页之顶部轮播
  2. android蓝牙开发————实现服务端客户端通信
  3. android彻底结束进程的两种方法
  4. 【Android】进程通信IPC——ContentProvider
  5. android init 进程分析 (4 属性服务)
  6. android开发新浪微博客户端 完整攻略 [新手必读]

随机推荐

  1. FragmentTabhost记录
  2. A Visual Guide to Android(安卓)GUI Wid
  3. Android(安卓)Location 和 Sensor的使用,
  4. Android(安卓)ListView的子项的随意拖动
  5. Android(安卓)解析excel文件
  6. Android(安卓)P(api28) 不支持 http 协议
  7. Android(安卓)Contacts(一)—— 读取联系人
  8. android 安卓创建文件夹
  9. android(3)(android五大布局)
  10. android activity 单元测试总结