跨应用绑定Service并通信:

1、(StartServiceFromAnotherApp)AIDL文件中新增接口:

  void setData(String data);

  AppService文件中实现接口:

  public IBinder onBind(Intent intent) {
    return new IAppServiceRomoteBinder.Stub() {
      @Override
      public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
      }
      @Override
      public void setData(String data) throws RemoteException {
      }
    };
  }

2、(StartServiceFromAnotherApp)修改AppService内部数据:

  private String data = "默认信息";

  public void setData(String data) throws RemoteException {
    AppService.this.data = data;
  }

3、((StartServiceFromAnotherApp)AppService

  onCreat写线程,onDestroy中销毁,每隔一秒输出data,方便测试内部数据的变化:

  private boolean running = false;

   public void onCreate() {
    super.onCreate();
    System.out.println("Service started");

    new Thread(){
      @Override
      public void run() {
        super.run();
        running = true;
        while(running){
          System.out.println(data);
        try {
          Thread.sleep(1000);
          } catch (InterruptedException e) {
            e.printStackTrace();
            }
        }
      }
    }.start();
  }

  public void onDestroy() {
    super.onDestroy();
    System.out.println("Service destroy");
    running = false;
  }

4、(AnotherApp)主布局:

  <EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="这是另一个应用中的数据"
    android:id="@+id/etInput" />

  <Button
    android:text="同步数据到绑定的服务中"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/btnSync" />

5、(AnotherApp)进入MainActivity,按钮及输入文本的监听:

   private EditText etInput;

   etInput = (EditText) findViewById(R.id.etInput);

   findViewById(R.id.btnSync).setOnClickListener(this);

6、如何通过Binder方便地执行远程函数?把StartServiceFromAnotherApp中AIDL文件拷贝到AnotherApp中,保持包名一致。

7、(AnotherApp)进行数据最后处理:

  private IAppServiceRomoteBinder binder = null;

  case R.id.btnUnbindService:
    unbindService(this);
    binder = null;
    break;
  case R.id.btnSync:
    if(binder != null){
      try {
      binder.setData(etInput.getText().toString());
        } catch (RemoteException e) {
           e.printStackTrace();
        }
    }

  

  public void onServiceConnected(ComponentName name, IBinder service) {
      //binder = IAppServiceRomoteBinder(service); 两个类定义所在地址不一样,不能强制类型转换
      binder = IAppServiceRomoteBinder.Stub.asInterface(service);
  }

Android中AIDL的理解与使用(二)——跨应用绑定Service并通信的更多相关文章

  1. Android中AIDL的理解与使用(一)——跨应用启动/绑定Service

    AIDL(Android Interface Definition Language)--安卓接口定义语言 一.startService/stopService 1.同一个应用程序启动Service: ...

  2. Android中Context的理解及使用(二)——Application的用途和生命周期

    实现数据共享功能: 多个Activity里面,可以使用Application来实现数据的共享,因为对于同一个应用程序来说,Application是唯一的. 1.实现全局共享的数据App.java继承自 ...

  3. (七)Android中AIDL的应用与理解

    一.跨应用启动Service Intent serviceIntent=new Intent();serviceIntent.setComponent(new ComponentName(" ...

  4. Android中AIDL通信机制分析

    一.背景 ·1.AIDL出现的原因 在android系统中,每一个程序都是运行在自己的进程中,进程之间无法进行通讯,为了在Android平台,一个进程通常不能访问另一个进程的内存空间,所以要想对话,需 ...

  5. Android 中AIDL的使用与理解

    AIDL的使用: 最常见的aidl的使用就是Service的跨进程通信了,那么我们就写一个Activity和Service的跨进程通信吧. 首先,我们就在AS里面新建一个aidl文件(ps:现在AS建 ...

  6. Android笔记——Android中数据的存储方式(二)

    我们在实际开发中,有的时候需要储存或者备份比较复杂的数据.这些数据的特点是,内容多.结构大,比如短信备份等.我们知道SharedPreferences和Files(文本文件)储存这种数据会非常的没有效 ...

  7. Android中一个经典理解误区的剖析

    今天,在Q群中有网友(@广州-包晴天)发出了网上的一个相对经典的问题,问题具体见下图. 本来是无意写此文的,但群里多个网友热情不好推却,于是,撰此文予以分析. 从这个问题的陈述中,我们发现,提问者明显 ...

  8. Android中的一些基础知识(二)

    这几天在回顾Android的基础知识,就把一些常见的知识点整理一下,以后忘了也可以翻出来看一看. 简单介绍一下Activity的生命周期 在API文档中对生命周期回调的函数描述的很详细,这里我只是翻译 ...

  9. Android笔记(四十) Android中的数据存储——SQLite(二) insert

    准备工作: 我们模拟一个注册的页面,先看UI 我们需要创建一个数据库:user,数据库包含表user,user表包含字段id.username.password.mobilephone MainAct ...

随机推荐

  1. win10关机指示灯亮解决办法

    新上市的win10,大家都迫不及待的装上了,但是却遇到了很多问题. 对于win10关机指示灯亮的解决办法有下面几种. 指示灯 如果是只有电源的指示灯亮,拔掉电源即可. 如果是信号灯和其他的指示灯都亮原 ...

  2. [No000094]SVN学习笔记4-版本库概念与部分日常操作

    基本概念 版本库 Subversion 使用集中的数据库,它包含了所有的版本控制文件及其完整历史.这个数据库就是版本库.版本库通常位于运行 Subversion 服务器的文件服务器上,向 Subver ...

  3. [LeetCode] Rotate Array 旋转数组

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array  ...

  4. MVC系列——MVC源码学习:打造自己的MVC框架(三:自定义路由规则)

    前言:上篇介绍了下自己的MVC框架前两个版本,经过两天的整理,版本三基本已经完成,今天还是发出来供大家参考和学习.虽然微软的Routing功能已经非常强大,完全没有必要再“重复造轮子”了,但博主还是觉 ...

  5. VUE---Missing space before function parentheses

    解决方法:

  6. Beta版本冲刺计划及安排

    经过紧张的Alpha阶段,很多组已经从完全不熟悉语言和环境,到现在能够实现初步的功能.下一阶段即将加快编码进度,完成系统功能.强化软件工程的体会.Beta阶段的冲刺时间为期七天,安排在2016.12. ...

  7. mtk flash tool,Win7 On VirtualBox

    SP_Flash_Tool_exe_Windows_v5.1624.00.000 Win7 在 VirtualBox, 安裝 mtk flash tool, v5.1628 在燒錄時會 fail. v ...

  8. logback logback.xml常用配置详解 <filter>

    <filter>: 过滤器,执行一个过滤器会有返回个枚举值,即DENY,NEUTRAL,ACCEPT其中之一.返回DENY,日志将立即被抛弃不再经过其他过滤器:返回NEUTRAL,有序列表 ...

  9. 【BZOJ 4580】【Usaco2016 Open】248

    http://www.lydsy.com/JudgeOnline/problem.php?id=4580 区间dp,f(i,j)表示区间[i,j]全部合成一个数,这个数是多少. 可以归纳证明[i,j] ...

  10. 【转】封装原生JS实现Ajax

    function createXHR() { if (window.XMLHttpRequest) { //IE7+.Firefox.Opera.Chrome 和Safari return new X ...