4-AIII--Service跨进程通信:aidl
零、前言
[1]. aidl:Android Interface definition language(安卓接口定义语言),目的:提供进程间的通信接口
[2]. 一个应用提供服务:称为服务应用,一个使用服务:称为客户应用
[3]. 解决客户应用如何调用服务应用的服务方法时,便是aidl用武之地
[4]. 服务端开启验证服务,客户端输入用户名和命名及数值,验证用户名:abc,密码:123,数值<5000
一、服务端代码实现:
1.com/toly1994/aiii_service/IPayAidlInterface.aidl
// IPayAidlInterface.aidl package com.toly1994.aiii_service; // Declare any non-default types here with import statements interface IPayAidlInterface { //暴露方法 boolean pay(String name, String pwd, int money); }
2.PayService服务
public class PayService extends Service { @Override public IBinder onBind(Intent arg0) { return new MyBinder(); } public class MyBinder extends IPayAidlInterface.Stub { @Override public boolean pay(String name, String pwd, int money) { return "abc".equals(name) && "123".equals(pwd) && money < 5000; } } }
3.注册:app/src/main/AndroidManifest.xml
<service android:name=".aidl.PayService" android:enabled="true" android:process=":push" android:exported="true"> <intent-filter> <action android:name="www.toly1994.com.pay"></action> </intent-filter> </service>
4.AidlActivity:启动服务
public class AidlActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startService(new Intent(this, PayService.class)); } }
二、客户端
1. MainActivity
public class MainActivity extends AppCompatActivity { @BindView(R.id.et_acc) EditText mEtAcc; @BindView(R.id.et_pass) EditText mEtPass; @BindView(R.id.et_num) EditText mEtNum; @BindView(R.id.btn_buy) Button mBtnBuy; private ServiceConnection mConn; private IPayAidlInterface mService; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); bindService(); } /** * 绑定远程服务 */ private void bindService() { Intent intent = new Intent(); //坑点:5.0以后要加 服务包名,不然报错 intent.setPackage("com.toly1994.aiii_service"); intent.setAction("www.toly1994.com.pay"); mConn = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { mService = IPayAidlInterface.Stub.asInterface(service); } @Override public void onServiceDisconnected(ComponentName name) { } }; bindService(intent, mConn, BIND_AUTO_CREATE); } @OnClick(R.id.btn_buy) public void onViewClicked() { try { boolean isPay = mService.pay(mEtAcc.getText().toString(), mEtPass.getText().toString(), Integer.parseInt(mEtNum.getText().toString())); Toast.makeText(this, isPay ? "购买成功" : "购买失败", Toast.LENGTH_SHORT).show(); } catch (RemoteException e) { e.printStackTrace(); } } @Override protected void onDestroy() { unbindService(mConn); super.onDestroy(); } }
2.com/toly1994/aiii_service/IPayAidlInterface.aidl
// IPayAidlInterface.aidl package com.toly1994.aiii_service; // Declare any non-default types here with import statements interface IPayAidlInterface { //暴露方法 boolean pay(String name, String pwd, int money); }
附录、客户端布局文件:activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <EditText android:id="@+id/et_acc" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入账号" /> <EditText android:id="@+id/et_pass" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入密码" /> <EditText android:id="@+id/et_num" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="1" android:hint="请输入数额" /> <Button android:id="@+id/btn_buy" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="确定" /> </LinearLayout>
注意坑点:5.0以后用action启动服务要加服务所在应用的包名,不然报错
后记、
1.声明:
[1]本文由张风捷特烈原创,转载请注明
[2]欢迎广大编程爱好者共同交流
[3]个人能力有限,如有不正之处欢迎大家批评指证,必定虚心改正
[4]你的喜欢与支持将是我最大的动力
2.连接传送门:
更多安卓技术欢迎访问:安卓技术栈
我的github地址:欢迎star
简书首发,腾讯云+社区同步更新
张风捷特烈个人网站,编程笔记请访问:http://www.toly1994.com
3.联系我
QQ:1981462002
邮箱:1981462002@qq.com
微信:zdl1994328
4.欢迎关注我的微信公众号,最新精彩文章,及时送达:
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
3-AIII--Service绑定服务基于接口调用方法
零、前言 1.上篇中MyBinder暴漏在外,亲自上阵,执行方法。Binder是连接者,能不出面尽量不出面,一个"电话(接口)"搞定的事就不要让他老人家(MyBinder)亲自跑一趟。面向接口,隐藏不必要的对象。 2.说服务怎能少得了音乐播放器,这里只写写伪方法,后面在多媒体篇会来完善。 基于接口调用方法.gif 一、代码实现 1.服务方法接口 /** * 作者:张风捷特烈 * 时间:2018/8/25 0025:11:09 * 邮箱:1981462002@qq.com * 说明:服务方法接口 */ public interface IMService { /** * 播放音乐 */ void playMusic(); /** * 暂停音乐 */ void pauseMusic(); /** * 继续播放音乐 */ void rePlayMusic(); } 2.音乐服务:MusicService public class MusicService extends Service { private static final String TAG = "MusicService"; @...
- 下一篇
TI--安卓运行时权限封装
零、前言: [1]两行代码解决运行时权限 [2]用户拒绝后显示提示信息,有再次验证功能 [3]验证成功回调 一个权限效果: 一个权限效果.gif 六个权限演示: 六个权限演示.gif 一、代码实现 1.单一权限申请 public class MainActivity extends PermissionActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //单权限申请 applyPermissions(_WRITE_EXTERNAL_STORAGE()); } @Override protected void permissionOk() { ToastUtil.showAtOnce(MainActivity.this, "授权通过"); } } 2.多权限:常用的六组运行时权限 applyPermissions( _WRITE_EXTERN...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
-
Docker使用Oracle官方镜像安装(12C,18C,19C)
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- CentOS8编译安装MySQL8.0.19
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- MySQL8.0.19开启GTID主从同步CentOS8
- CentOS7,8上快速安装Gitea,搭建Git服务器
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
推荐阅读
最新文章
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- CentOS6,CentOS7官方镜像安装Oracle11G
- SpringBoot2整合Redis,开启缓存,提高访问速度
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- Hadoop3单机部署,实现最简伪集群
- MySQL8.0.19开启GTID主从同步CentOS8
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果