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. flot_js_$用法解释

    $用法解释 $在JS中本身只是一个符号而异,在JS里什么也不是.但在JS应用库JQUERY的作者将之做为一个自定义函数名了,这个函数是获取指定网页元素的函数,使用非常之频繁,所以好多新手不知道,还以为 ...

  2. jni

    http://www.cnblogs.com/likwo/archive/2012/05/21/2512400.html

  3. Calculation

    定义一个Strategy接口,其中定义一个方法,用于计算 using System; using System.Collections.Generic; using System.Linq; usin ...

  4. drds 分库表的创建速记

    关键词 :dbpartition by hash(`INVESTOR_APPLY_ID`) 格式 :dbpartition by hash(分库字段) 创建例子: CREATE TABLE `BB_J ...

  5. 夺命雷公狗---DEDECMS----13dedecms首页的完成

    我们的dedecms搭建起来后直接复制templets的目录复制一份,如下所示: 然后进入templets目录里面,然后再将default里面的东西都给干掉,然后将我们预先准备好的首页放进来,代码如下 ...

  6. 安装Debian的正确方法

    一.说明: Debian7.0.0的安装镜像文件有3个DVD,安装基本系统只用到第一个镜像文件,即DVD1 其它镜像文件是附带的软件包. 附Debian 7.0.0系统镜像下载地址: 32位:http ...

  7. Server编解码 解决Response.Redirect方法传递汉字丢失或乱码

  8. 【DP水题】投票问题(二)

    投票问题(一) [试题描述] 欧阳文和欧阳武竞选学联主席,汪梁森负责唱票,共有m+n张,结果欧阳文获胜,已知欧阳文和欧阳武分别获得 m 张票和 n 张票(m>n).现在请你计算在唱票过程中欧阳文 ...

  9. 继承(引用~析构~virtual)

    [1]程序1 #include <iostream> using namespace std; class Base { private: int m_nBase; public: Bas ...

  10. 鸟哥的linux私房菜学习记录之正则表达式

    正则表达式具有强大的字符串处理能力,常常用来搜索删除和替换字符串,用途很广. sed awk数据处理工具 diff,cmp,patch,pr文档对比工具