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. 类名.class, class.forName(), getClass()区别

    1:Class cl=A.class; JVM将使用类A的类装载器, 将类A装入内存(前提是:类A还没有装入内存),不对类A做类的初始化工作.返回类A的Class的对象. 2:Class cl=对象引 ...

  2. PAT乙级 1022. D进制的A+B (20)

    1022. D进制的A+B (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 输入两个非负10进制整数A和 ...

  3. OpenGL拾取注意事项

    GLFrame框架本身不支持拾取,需要自己实现.以下代码是实现拾取功能的注意事项: void Test::doSelection(int xPos, int yPos) { GLfloat aspec ...

  4. sql2005镜像实现

    -- ===========================================    -- 无论是主体服务器.镜像服务器, 还是见证服务器    -- 除特别说明外,均需要保证下面的操作 ...

  5. Spring+SpringMVC+MyBatis)

    用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合的 ...

  6. SQL Server 最小化日志操作解析,应用

    Sql Server 中数据库在BULK_LOGGED/SIMPLE模式下的一些操作会采用最小化日志的记录方式,以减小tran log落盘日志量从而提高整体性能. 这里我简单介绍下哪些操作在什么样的情 ...

  7. 电脑配置 eclipse 环境变量

    先找到电脑环境变量 我的电脑——属性——高级系统设置——环境变量 path里面需要两个环境 第一个Java 环境 C:\Program Files\Java\jdk1.8.0_40\bin; 第二个a ...

  8. 第三方过滤器在TVideoGrabber中的使用

    在TVideoGrabber中可以使用第三方过滤器,并可插入到预览.录制或回放流中,添加到列表里. 要在一个图像中中应用一个过滤器,需要像下面的例子中一样调用 ThirdPartyFilter_Add ...

  9. 我是如何对网站CSS进行架构的

    by zhangxinxu from http://www.zhangxinxu.com 本文地址:http://www.zhangxinxu.com/wordpress/?p=944 一.写在前面的 ...

  10. 【python cookbook】【字符串与文本】13.对齐文本字符串

    问题:以某种对齐方式将文本做格式化处理 解决方案: 1.针对字符串:ljust().rjust().center()方法 2.针对任何值,更加通用的:format()  更多内容:https://do ...