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. ubuntu下修改键位

    尴尬的背景: 服役5年的笔记本,最近键盘失灵,部分键位彻底失去响应.最蛋疼的是左右方向键都不能用了 ○| ̄|_ 解决方案是,通过xmodmap命令,用其他相对鸡肋些的键位替代方向键. 1 查看各个键位 ...

  2. Spring MVC之@RequestParam @RequestBody @RequestHeader 等详解

    (转自:http://blog.csdn.net/walkerjong/article/details/7946109#) 引言: 接上一篇文章,对@RequestMapping进行地址映射讲解之后, ...

  3. monkey之monkey命令详解

    四大类-- 常用选项.事件选项.约束选项.调试选项 1.常用选项 --help:打印帮助信息 -v:指定打印信息的详细级别,一个-v增加一个级别 ,默认级别为 0 .用于指定反馈信息级别(信息级别就是 ...

  4. Linux程序包管理.md

    rpm 简介 RPM包管理员(简称RPM,全称为The RPM Package Manager)是在Linux下广泛使用的软件包管理器.RPM此名词可能是指.rpm的文件格式的软件包,也可能是指其本身 ...

  5. JavaSript模块规范 - AMD规范与CMD规范介绍

    JavaSript模块化   在了解AMD,CMD规范前,还是需要先来简单地了解下什么是模块化,模块化开发?       模块化是指在解决某一个复杂问题或者一系列的杂糅问题时,依照一种分类的思维把问题 ...

  6. 【深度学习Deep Learning】资料大全

    最近在学深度学习相关的东西,在网上搜集到了一些不错的资料,现在汇总一下: Free Online Books  by Yoshua Bengio, Ian Goodfellow and Aaron C ...

  7. 俄罗斯方块C#版

    using System; using System.Windows.Forms; using System.Drawing; using System.Media; class me : Form ...

  8. python2.7初学(〇)

    为什么学习Python Python为我们提供了非常完善的基础代码库,覆盖了网络.文件.GUI.数据库.文本等大量内容,被形象地称作“内置电池(batteries included)”.而且pytho ...

  9. 【.NET】Cookie操作类

    public static class CookiesHelper { /// <summary> /// Cookies赋值 /// </summary> /// <p ...

  10. com.panie 项目开发随笔_前后端框架考虑(2016.12.8)

    (一) 近日和一同学联系,说了我想要做一个网站的打算.她很感兴趣.于是我们协商了下,便觉得一起合作.她写前端,我写后台.因为我对于前端样式设计并不怎么熟悉. (二) 我们决定先做一个 个人博客. 网上 ...