AIDL(Android Interface Definition Language)——安卓接口定义语言

一、startService/stopService

1、同一个应用程序启动Service:

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    startService(new Intent(this,AppService.class));  //启动Service
  }
  protected void onDestroy() {
    super.onDestroy();
    stopService(new Intent(this,AppService.class));  //停止Service
  }

2、跨应用启动service:

  通过Action的隐式Intent启动Service在安卓5.0以前的版本是可以实现跨应用启动Service的,安卓5.0以后的版本若想实现

跨应用启动Service的功能必须使用显示Intent。

  public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Intent serviceIntent;
    
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      serviceIntent = new Intent();
      serviceIntent.setComponent(new ComponentName("com.w.startservicefromanotherapp","com.w.startservicefromanotherapp.AppService"));

      findViewById(R.id.btnStartService).setOnClickListener(this);
      findViewById(R.id.btnStopService).setOnClickListener(this);
  }

    public void onClick(View v) {
      switch (v.getId()){
        case R.id.btnStartService:
          // Intent i = new Intent();
          //i.setComponent(new ComponentName("com.w.startservicefromanotherapp","com.w.startservicefromanotherapp.AppService"));

          //显示 Intent被启动程序的包名,被启动服务的类名
          startService(serviceIntent);
          break;
        case R.id.btnStopService:
          stopService(serviceIntent);
          break;
         }
    }
  }

二、bindService/unbindService

跨应用绑定Service:AIDL——用于在多个应用程序之间进行通信

1、在StartServiceFromAnotherApp新建AIDL文件IAppServiceRomoteBinder,会全自动生成相关的类。

2、在返回Binder时(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 {  }
    };
  }

3、在AnotherApp项目中绑定这个服务:

  findViewById(R.id.btnBindService).setOnClickListener(this);
  findViewById(R.id.btnUnbindService).setOnClickListener(this);

  case R.id.btnBindService:
    bindService(serviceIntent,this, Context.BIND_AUTO_CREATE);
    break;
  case R.id.btnUnbindService:
    unbindService(this);
    break;

  @Override
  public void onServiceConnected(ComponentName name, IBinder service) {
    System.out.println("Bind Service");
    System.out.println(service);  //显示IBinder service
  }

  @Override
  public void onServiceDisconnected(ComponentName name) {  }

Android中AIDL的理解与使用(一)——跨应用启动/绑定Service的更多相关文章

  1. Android中AIDL的理解与使用(二)——跨应用绑定Service并通信

    跨应用绑定Service并通信: 1.(StartServiceFromAnotherApp)AIDL文件中新增接口: void setData(String data); AppService文件中 ...

  2. Android中AIDL通信机制分析

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

  3. Android 中AIDL的使用与理解

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

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

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

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

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

  6. 彻底明确Android中AIDL及其使用

    1.为什么要有AIDL? 不管学什么东西,最先得弄明确为什么要有这个东西.不要说存在即是合理.存在肯定合理,可是你还是没有明确. 对于AIDL有一些人的浅显概念就是,AIDL能够跨进程訪问其它应用程序 ...

  7. 【转】android中layout_weight的理解

    android Layout_weight的理解 SDK中的解释: Indicates how much of the extra space in the LinearLayout will be ...

  8. (四)Android中Context的理解与使用

    一.Context的作用 Context可用于访问全局资源. public class MainActivity extends Activity { private TextView tv; @Ov ...

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

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

随机推荐

  1. XP本地连接正常无法上网的解决方法

    原文: http://www.doc88.com/p-599590609730.html

  2. WPF 自定义IconButton

    自定义一个按钮控件 按钮控件很简单,我们在项目中有时把样式封装起来,添加依赖属性,也是为了统一. 这里举例,单纯的图标控件怎么设置 1.UserControl界面样式 <UserControl ...

  3. 项目游戏开发日记 No.0x000004

    14软二杨近星(2014551622) 还有两周就要交项目了, 我们的作品, 作为作业, 好吧, 其实它完成了接近50%, (only the first bate), 其实也是各种各种忙, 然后才赶 ...

  4. 【译】Getting Physical With Memory

    当我们试图去了解复杂系统时,去除其抽象层,直接关注最底层,我们会更容易去理解.使用这种方法,我们来看一下内存和 I/O 接口的最简单和基础的层:处理器和总线的接口.这些细节是更上层问题的基础,例如线程 ...

  5. Markdown编辑器语法指南2

    人的一切痛苦, 本质上都是对自己的无能的愤怒. --王小波 1 Markdown编辑器的基本用法 1.1 代码 如果你只想高亮语句中的某个函数名或关键字,可以使用 `function_name()` ...

  6. WWW读取安卓外部音乐文件

    需求分析 使用Everyplay(2121-1540版本)录屏,在升级SDK之后,遇到个问题,调用安卓原生的mediaplay进行播放音乐,在录屏时无法录制到声音,所以想到的解决办法是在Unity中播 ...

  7. Lambert(朗伯)光照模型 和Half Lambert的区别

    Lambert-它不包括任何任何镜面属性,对粗糙物体来说,这项属性是非常有用的,它不会反射出周围的环境.Lambert材质可以是透明的,在光线追踪渲染中发生折射,但是如果没有镜面属性,该类型就不会发生 ...

  8. Salesforce Apex页面中调用远端网络服务

    本文介绍了Salesforce Apex页面中调用远端网络服务的实现过程. ### 注册远端网络服务 在使用Apex代码调用远端网络服务之前,首先需要在Salesforce中注册远端网络服务地址, 本 ...

  9. ESLint规则配置说明

    "no-alert": 0,//禁止使用alert confirm prompt "no-array-constructor": 2,//禁止使用数组构造器 & ...

  10. Linux-./configure: error: the HTTP rewrite module requires the PCRE library.

    这个问题是要解决: yum -y install pcre-devel 然后在yum的时候发现出错.问题是我的linux不能上网. 这个问题搞得烦死了.和主机能ping.但是就是不能上网: ping ...