android 中activity调用远程service中的方法之 aidl的使用
服务端:只有服务,没有界面
1.编写interface文件,复制到 .aidl 文件中,并去掉其中的public 等修饰符。系统会自动在gen目录下生成对应的java文件 (对应本地调用中的接口文件)
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAkoAAAB/CAYAAAAQC+2uAAAABHNCSVQICAgIfAhkiAAAEVpJREFUeF7t3U1u5LgVAGA7yMKLDpBcN5hlkFPkHDlKtllmmQDdQPfOAQMQIZ5JilSpVPr5DAzGEsnH9z7KLo5UU37/29//8fnmiwABAgQIECBA4IvA+8/vPz8/vn18aRg58evHr7e1Y0fi60OAAAECBAgQeKXA7145ubkJECBAgAABAkcWsFE68urIjQABAgQIEHipwOE3Su/v7y8FuuPkyXzJvWzP/ZfG3NFSzQQIECBwboHhjdJvf/7tLf3ji0AU+Pz8fEv/+CJAgAABAlcTGNoopQ3SX/76l6vVrp6GgI1PA8ZpAgQIELidwO9HKh7dJMVHL/EuQ9nea0s5xfZ0Lo8v22ZilnGXcm25tMa1zrfi5PNL41r1pfPRoWZWm78XM/evxYq51mK3zuWxtbitMc4TIECAAIFXCwxtlGaTzC+G8cW89cI+8iLa6tOLOdqW6ou51mpu5VD2bdVeizcyrlXDUrxeeytmmXscH30e2TTF2I4JECBAgMBRBTbfKJUvwrHo3ovr0rhWey9mnL88XjuulUeK3Wvr5dIb18szb17iJqY3V2rrxVwau7a9V+PamMYRIECAAIFnC2y+UWolHF/MZ16s04tsHJ/miedizPI4vlDH41berzzfqy+bzObXizkbS38CBAgQIHB1gaE3c2+NEDc0KX7tXDnv0sagNj6Nyf/EGmr9Y5/a8SPj1o4d8Ym5prmW5ltqjzHz8ZpxI/m05nOeAAECBAi8SmDoT5jUPhYgvcE7/gmT+AIa79r02ltt6XwZp3Zc4uW+MV7qE+NE9JhvbE/HMW45X4wfj2MOvXh57tZ8ZXst7zyu15ZjzJql/uU6xBx7ddbyyXn4NwECBAgQOJrA0EaplXRto3SUF8K4oUo11M61atvr/KM5P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jgI2Sjtim4oAAQIECBA4l4CN0rnWS7YECBAgQIDAjgI2Sjtim4oAAQIECBA4l4CN0rnWS7YECBAgQIDAjgI2Sjtim4oAAQIECBA4l4CN0rnWS7YECBAgQIDAjgI2Sjtim4oAAQIECBA4l4CN0rnWS7YECBAgQIDAjgI2Sjtim4oAAQIECBA4l4CN0rnWS7YECBAgQIDAjgI2Sjtim4oAAQIECBA4l4CN0rnWS7YECBAgQIDAjgI2Sjtim4oAAQIECBA4l4CN0rnWS7YECBAgQIDAjgI2Sjtim4oAAQIECBA4l4CN0rnWS7YECBAgQIDAjgI2Sjtim4oAAQIECBA4l4CN0rnWS7YECBAgQIDAjgI2Sjtim4oAAQIECBA4l4CN0rnWS7YECBAgQIDAjgI2Sjtim4oAAQIECBA4l4CN0rnWS7YECBAgQIDAjgI2Sjtim4oAAQIECBA4l4CN0rnWS7YECBAgQIDAjgI2Sjtim4oAAQIECBA4l4CN0rnWS7YECBAgQIDAjgI2Sjtim4oAAQIECBA4l4CN0rnWS7YECBAgQIDAjgI2Sjtim4oAAQIECBA4l4CN0rnWS7YECBAgQIDAjgI2Sjtim4oAAQIECBA4l4CN0rnWS7YECBAgQIDAjgL/BRk9rrnZqPm2AAAAAElFTkSuQmCC" alt="" />
2.编写service,其中内部类的自定义bind 只需要继承Stub即可。(本地调用则需要继承Bind 并实现 interface接口)
public class PayService extends Service { @Override
public IBinder onBind(Intent intent) {
return new Pay();
}
public class Pay extends Stub {
@Override
public void pay(String userName, String password, String sendToAccount, float money) throws RemoteException {
toPay(userName, password, sendToAccount, money);
}
}
public void toPay(String userName, String password, String sendToAccount, float money){
System.out.println(userName + "@" + password + " to " + sendToAccount + "$" + money);
} }
3.清单文件中注册,添加intent-filter 项,以便于被调用
<service android:name="cn.pay.service.PayService" >
<intent-filter>
<action android:name="cn.pay.service.PayService" />
</intent-filter>
</service>
========================================================分隔线========================================================
客户端:
在activity中自定义ServiceConnection的内部类。转化接口实例的方法用 接口类名.Stub.asInterface(service) (本地调用是直接将service强转为接口实例)
package com.example.aidlpayclient; import cn.pay.aidl.IPayService;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //绑定服务
conn=new MyServiceConnection();
bindService(new Intent("cn.pay.service.PayService"), conn, Context.BIND_AUTO_CREATE);
} private MyServiceConnection conn = null;
private IPayService iPayService; private class MyServiceConnection implements ServiceConnection { @Override
public void onServiceConnected(ComponentName name, IBinder service) {
iPayService = IPayService.Stub.asInterface(service);
System.out.println("绑定成功");
} @Override
public void onServiceDisconnected(ComponentName name) {
iPayService = null;
} } public void pay(View v) {
System.out.println("begin pay");
try {
//调用方法.
iPayService.pay("a", "b", "c", 100);
} catch (RemoteException e) {
e.printStackTrace();
}
System.out.println("end pay");
} }
android 中activity调用远程service中的方法之 aidl的使用的更多相关文章
- android 中activity调用本地service中的方法。
1.自定义一个接口,暴露服务中的方法 public interface IService { /**服务中对外暴露的方法 */ void methodInService();} 2.自定一 ...
- Android Activity与远程Service的通信学习总结
当一个Service在androidManifest中被声明为 process=":remote", 或者是还有一个应用程序中的Service时,即为远程Service, 远程的意 ...
- html中引入调用另一个html的方法
html中引入调用另一个html的方法,尝试了好几种,都列出来: 其中第一种是最好的,其他的方法,可以尝试看看,是不是适合你当前项目 一.div+$("#page1").load( ...
- 【安卓面试题】在一个Activity启动另一个Activity和在Service中启动一个Activity有什么区别
在Activity中可以直接使用Intent启动另一个Activity 显式Intent intent = new Intent(context, activity.class) 隐式 Intent ...
- Android悬浮框,在Service中打开悬浮窗;在Service中打开Dialog;
文章介绍了如何在Service中显示悬浮框,在Service中弹出Dialog,在Service中做耗时的轮询操作: 背景需求: 公司的项目现在的逻辑是这样的:发送一个指令,然后3秒一次轮询去查询这个 ...
- Android 儿子Activity在启动过程中的流程组件 && 儿子Activity在一个新的进程组件启动过程
1.儿子Activity在启动过程中的流程组件 在Android Activity启动过程http://blog.csdn.net/jltxgcy/article/details/35984557一文 ...
- 项目中Ajax调用ashx页面中的Function的实战
前台页面: 使用几个display=none的空间存储DropdownList中的值,点击Search Button后刷新页面再次给DropdownList赋值使用 <%@ Page Langu ...
- C代码中如何调用C++ C++中如何调用C
注意这里的C调用C++或者C++调用C意思是.c文件中调用.cpp文件中代码,或者相反. 集成开发环境如VC++6.0或者vs都是以文件后缀来区别当前要编译的是C代码还是C++代码,然后采用响应的编译 ...
- Magento中,调用静态块的几种方法
在后台创建一个order_form静态块Block Title :Order FormIdentifier :order_formStatus :EnabledContent :自定义内容 1.如果要 ...
随机推荐
- SqlServer基础:游标
记录下今天用到的游标: DECLARE @TempID INTDECLARE @Number INTSET @Number=1DECLARE myCursor CURSOR FOR SELEC ...
- android开源框架
1.volley 项目地址 https://github.com/smanikandan14/Volley-demo (1) JSON,图像等的异步下载: (2) 网络请求的排序(scheduli ...
- jboss漏洞导致服务器中毒
中毒现象 1. 网络出现拥塞,访问延迟增加. 2. 系统定时任务表中出现异常的定时任务. 3. 出现异常进程. 4. $JBOSS_HOME/bin或/root目录下出现大量的异常文件. 现象 ...
- tomcat 禁用不安全的http请求模式 .
HTTP服务器至少应该实现GET和HEAD方法,其他方法都是可选的.当然,所有的方法支持的实现都应当符合下述的方法各自的语义定义.此外,除了上述方法,特定的HTTP服务器还能够扩展自定义的方法. ht ...
- [RGEOS]绘制多边形Polygon
绘制OGIS定义的Polygon public void DrawPolygon(Polygon pol, Brush brush, Pen pen, bool clip) { gc = Graphi ...
- yum只下载安装需要的rpm包
1.安装yum的插件yum-downloadonly yum -y install yum-downloadonly 2.使用 yum -y install somepackges --downloa ...
- Entity FrameWork初始化数据库的四种策略
程序猿就是苦逼,每天还得分出一些时间去写博文.天真的很热,今天就随便写一点啦! 1.EF初始化数据库的四中策略 EF可以根据项目中的模型自动创建数据库.下面我们就分类看看Entity Framewor ...
- 一款不错的多选下拉列表利器—— Ext.ux.form.SuperBoxSelect
在B/S系统中,下拉列表(select/dropdownlist/combobox)的应用随处可见,为了增强用户体验,开发人员也常常会做一些带联想功能的下拉列表, 特别是数据项比较多的时候,用户筛选起 ...
- Moment.js学习(一)源代码
本篇主要是学习Moment.js.类库源代码如下: 2.4版本. //! moment.js //! version : 2.4.0 //! authors : Tim Wood, Iskren Ch ...
- G面经prepare: Android Phone Unlock Pattern
1 2 3 4 5 6 7 8 9 只有中间没有其他键的两个键才能相连,比如1可以连 2 4 5 6 8 但不能连 3 7 9 但是如果中间键被使用了,那就可以连,比如5已经被使用了,那1就可以连9 ...