1.创建两个项目创建两个.aidl文件
2.在传递值的类里面创建Service并且返回接口:
服务返回值onBind
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return new IMyAidlInterface.Stub() {
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException { }
};
}
 
 
 

3.启动项目一service1将.aidl文件配置到项目中;

 
 
4.启动项目二service2:启动服务bindService
 
intentt = new Intent();
intentt.setComponent(new ComponentName("com.example.com.myapplication", "com.example.com.myapplication.MyService"));

启动服务

bindService(intentt, this, Context.BIND_AUTO_CREATE);
销毁服务
unbindService(this);

5.返回一个iMyAidlInterface接口

/**
*
* 连接
* @param name
* @param service
*/
@Override
public void onServiceConnected(ComponentName name, IBinder service) { iMyAidlInterface=IMyAidlInterface.Stub.asInterface(service); }

6.传递数据

if(iMyAidlInterface!=null) {
try {
iMyAidlInterface.setDate(et.getText().toString());
} catch (RemoteException e) {
e.printStackTrace();
}
}
7.接受数据并且应用
return new IMyAidlInterface.Stub() {
    @Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException { } @Override
public void setDate(String data) throws RemoteException { MyService.this.data=data;
}
};

 
 
 
 
 
 
一,简单的启动
 
1.创建一个项目,创建一个类继承Service
重写方法
 
 <service android:name="com.example.android_aidl.MyServer"
            android:process=":remote"
           android:exported="true"
            ></service>
 
2.创建一个项目,启动第一个项目的服务Service
 intentt = new Intent();
 intentt.setComponent(new ComponentName("com.example.android_aidl", "com.example.android_aidl.MyServer"));
参数为:启动项目的包名,启动项目包名的服务Service
startService(intentt);
stopService(intentt);
 
 
 
二,跨应用绑定Service:AIDL:android接口定义语言
 
创建.AIDL文件
 
服务返回值onBind
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return new IMyAidlInterface.Stub() {
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException { }
};
}

启动服务

bindService(intentt, this, Context.BIND_AUTO_CREATE);
销毁服务
unbindService(this);
/**
* 连接成功
* @param name
* @param service
*/
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// 打印输出
Log.e("App2 BInd service", "StartService"); } /**
* 连接失败
* @param name
* @param
*/
@Override
public void onServiceDisconnected(ComponentName name) { }
 
二,通过:AIDL:传值:
 
创建两个项目中包名,类名一样的.aidl文件用来交互并且内容也一样
 
在p2里面向p里面传值:
 
按钮监听
if(iMyAidlInterface!=null) {
try {
iMyAidlInterface.setDate(et.getText().toString());
} catch (RemoteException e) {
e.printStackTrace();
}
}
返回时接受的接口
@Override
public void onServiceConnected(ComponentName name, IBinder service) { iMyAidlInterface=IMyAidlInterface.Stub.asInterface(service); }
在p里面的Service中实现接口并且传值
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return new IMyAidlInterface.Stub() {
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException { } @Override
public void setDate(String data) throws RemoteException { MyService.this.data=data;
}
};
}

在Myservic中使用:

 
@Override
public void onCreate() {
super.onCreate(); new Thread(new Runnable() {
@Override
public void run() { run=true;
while (run){ try {
Thread.sleep(1000);
Log.e("service", data);
} catch (InterruptedException e) {
e.printStackTrace();
} } }
}).start(); Log.e("service", "startServier");
}
 
 

Android 中 Service AIDL使用的更多相关文章

  1. Android 中的AIDL,Parcelable和远程服务

    Android 中的AIDL,Parcelable和远程服务      早期在学习期间便接触到AIDL,当时对此的运用也是一撇而过.只到近日在项目中接触到AIDL,才开始仔细深入.AIDL的作用    ...

  2. Android中Service的使用详解和注意点(LocalService)

    Android中Service的使用详解和注意点(LocalService) 原文地址 开始,先稍稍讲一点android中Service的概念和用途吧~ Service分为本地服务(LocalServ ...

  3. Android中Service(服务)详解

    http://blog.csdn.net/ryantang03/article/details/7770939 Android中Service(服务)详解 标签: serviceandroidappl ...

  4. Android中Service的一个Demo例子

    Android中Service的一个Demo例子  Service组件是Android系统重要的一部分,网上看了代码,很简单,但要想熟练使用还是需要Coding.  本文,主要贴代码,不对Servic ...

  5. 【转载】Android Studio Service AIDL 详解

    公司产品之前IM这块存在很多问题,消息到达率低,加上协议上有些问题,丢消息频繁,所以需要重构IM,AIDL不能解决以上问题.好吧!那AIDL可以解决什么问题?什么是AIDL? 什么是AIDL? AID ...

  6. Android中Service 使用详解(LocalService + RemoteService)

    Service 简介: Service分为本地服务(LocalService)和远程服务(RemoteService): 1.本地服务依附在主进程上而不是独立的进程,这样在一定程度上节约了资源,另外L ...

  7. android中的AIDL进程间通信

    关于IPC应该不用多介绍了,Android系统中的进程之间不能共享内存,那么如果两个不同的应用程序之间需要通讯怎么办呢?比如公司的一个项目要更新,产品的需求是依附于当前项目开发一个插件,但是呢这个插件 ...

  8. Android中Service深入学习

    概述 1.当用户在与当前应用程序不同的应用程序时,Service可以继续在后台运行. 2.Service可以让其他组件绑定,以便和它交互并进行进程间通信. 3.Service默认运行在创建它的应用程序 ...

  9. android中的AIDL学习笔记

    一.定义 AIDL是用来解决进程间通信的(一般有四种方式:Activity.Service.ContentProvider.Broadcast Receiver),两个进程间无法直接通信,所以要用AI ...

随机推荐

  1. JavaScript中Date(日期对象),Math对象--学习笔记

    Date对象 1.什么是Date对象? 日期对象可以储存任意一个日期,并且可以精确到毫秒数(1/1000 秒). 语法:var Udate=new Date();  注:初始值为当前时间(当前电脑系统 ...

  2. angular ng-href

    farmApp.config([ '$compileProvider', function( $compileProvider ) { $compileProvider.aHrefSanitizati ...

  3. angular 倒计时

    $scope.countdown = ; var myTime = setInterval(function() { $scope.countdown--; $scope.$digest(); // ...

  4. 夺命雷公狗ThinkPHP项目之----企业网站5之栏目的添加(主要是图片上传)

    我们照老,先老搞定控CategoryController.class.php制器,代码如下所示: <?php namespace Admin\Controller; use Think\Cont ...

  5. linux计划任务运行php文件的方法

    在linux下,借助crontab,设置计划任务每天6点10分执行filename.php文件,写入一行时间到log日志中. 创建计划任务的脚本: dos2unix /path/to/filename ...

  6. Oracle 10g dataguard broker 配置

    环境: OS:rhel6.3_64bit DB:Oracle10gR2 ————————————————————————dataguard broker配置—————————————————————— ...

  7. JSP:一种服务器端动态页面技术的组件规范。

    java Servlet page:java服务器端页面,会增加服务器的压力. jsp文件会被容器转换成一个Servlet类然后执行. JSP页面中的注释: HTML注释:<!-- 注释中的ja ...

  8. iOS录音加播放.

    现在发现的事实有: 如果没有蓝牙设备, 那么可以用下面的方法边录音, 边放音乐: 在录音按钮按下的时候: _avSession = [AVAudioSession sharedInstance];   ...

  9. git 用法

    git init #创建仓库git add _filename #添加文件到缓冲区git commit -m "msg" #提交更新,从缓冲区提交到版本库git status #查 ...

  10. Java锁的种类

    转载自:---->http://ifeve.com/java_lock_see/ Java锁的种类以及辨析锁作为并发共享数据,保证一致性的工具,在JAVA平台有多种实现(如 synchroniz ...