AIDL使用教程一(传递默认类型数据)
来源:互联网 发布:打击电信网络犯罪总结 编辑:程序博客网 时间:2024/06/07 16:47
关于AIDL是什么,包括AIDL用途等概念性,可以网上搜一下,一大堆,在这里我直接说一下AIDL的用法,AIDL总起来说就是两个APK在不同的操作空间通信,传递数据,传递数据分为两种:
默认类型:Int float 等八大类型
非默认类型:自定义类继承自Parcelable
这篇博客先说一下 传递默认类型数据。
需求:有一个APP(服务端)内有一个类是用于计算两个数的和,还有一个APP(客户端)用户向服务端发送两个int型数据,然后服务端计算之后再返回给客户端,客户端展示在UI上。
先看下服务器APP的代码:
1:创建工程,然后在app上创建aidl文件,创建之后可以看到是跟Java是同一等级的,如下图;
2:在IMyAidlInterface.aidl文件内添加如下代码:
// IMyAidlInterface.aidlpackage com.app.test.aidlfirstproject;// Declare any non-default types here with import statementsinterface IMyAidlInterface { int add(int a, int b);}
3:创建ServerService.java类继承自Service,代码如下:
package com.app.test.aidlfirstproject;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.os.RemoteException;import android.support.annotation.Nullable;/** * Created by ${liumengqiang} on 2017/9/15. */public class ServerService extends Service{ @Nullable @Override public IBinder onBind(Intent intent) { return stub; } IMyAidlInterface.Stub stub = new IMyAidlInterface.Stub() { @Override public int add(int a, int b) throws RemoteException { return a + b; } };}
其实看到onBind返回stub对象 ,就可以知道它就是用于传递数据的。
在manifests文件中声明ServerService类
<service android:name=".ServerService"> <intent-filter> <action android:name="android.first.server"></action> </intent-filter> </service>
里面的action 的值 ,我们需要在客户端用到。
然后我们rebuild工程
到此服务器的app就完成了。接下来看看客户端:
4:创建ClientMoundle工程,我们可以随便创建一个aidl文件,然后将aidl下的包名改成服务器的包名,再将服务器下的IMyAidlInterface.aidl文件复制到客户端下,如下图所示:
切记:aidl里的任何东西都和服务端一致(包括名字);
5:然后就是MainActivity了,我们可以在XML中定义如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.app.test.clientmoudle.MainActivity"> <EditText android:id="@+id/edit_a" android:layout_width="match_parent" android:layout_height="50dp" android:hint="请输入a的值" android:textSize="25sp" android:inputType="number"/> <EditText android:id="@+id/edit_b" android:layout_width="match_parent" android:layout_height="50dp" android:hint="请输入b的值" android:textSize="25sp" android:inputType="number" android:layout_marginTop="50dp"/> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="25sp" android:gravity="center"/> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="25sp"/></LinearLayout>
然后在activity中如下:
package com.app.test.clientmoudle;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;import android.os.IBinder;import android.os.RemoteException;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import com.app.test.aidlfirstproject.IMyAidlInterface;public class MainActivity extends AppCompatActivity { private EditText editText_a; private EditText editText_b; private Button button; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText_a = (EditText) findViewById(R.id.edit_a); editText_b = (EditText) findViewById(R.id.edit_b); button = (Button) findViewById(R.id.button); textView = (TextView) findViewById(R.id.textView); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int a = Integer.parseInt(editText_a.getText().toString()); int b = Integer.parseInt(editText_b.getText().toString()); try { int add = iMyAidlInterface.add(a, b); textView.setText(a + " + " + b + " = " + add); } catch (RemoteException e) { e.printStackTrace(); } } }); /** * 连接服务器 */ Intent intent = new Intent(); intent.setAction("android.first.server");//服务器manifests中注册所填写的action intent.setPackage("com.app.test.aidlfirstproject");//服务器包名 bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE); } private IMyAidlInterface iMyAidlInterface;//创建的aidl文件名称 private ServiceConnection serviceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { /** * 如果iMyAidlInterface != null 连接成功 */ iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service); } @Override public void onServiceDisconnected(ComponentName name) { } };}
最后看下效果图:
下一篇讲述一下如果传递的是非默认类型数据,比如:传递一个User,
最后附上工程代码地址:http://download.csdn.net/download/lmq121210/9983985
然后是aidl传递非默认类型: http://blog.csdn.net/lmq121210/article/details/78042069
- AIDL使用教程一(传递默认类型数据)
- AIDL使用教程二(传递非默认类型数据)
- Activity之间传递数据(一)传递简单类型数据
- 【Android】使用AIDL传递用户自定义类型数据--附完整示例代码
- 【Android】AIDL原理以及基本类型数据传递
- IPC机制之AIDL传递基础类型数据
- 关于AIDL传递复杂数据(对象)
- AIDL数据传递 CopyOnWriteArrayList
- 使用AIDL实现进程间的通信之复杂类型传递 (转载liuhe688)
- AIDL传递自定义类型实现
- AIDL---使用AIDL实现进程间的通信之复杂类型传递 .
- AIDL学习笔记(一)----基本类型
- Android---45---使用AIDL Service传递复杂数据
- Android使用AIDL时传递复杂数据对象的写法
- AIDL服务-传递复杂数据
- Android使用AIDL传递对象
- COM,activex使用自定义类型传递数据
- AIDL进程间传递自定义类型参数
- 六个创建型模式2:工厂方法模式-Factory Method Pattern 【学习难度:★★☆☆☆,使用频率:★★★★★】
- Eclipse/myEclipse选中变量名,选择变量后该变量在当前文件中的使用都不变灰色了 标签: javaMyEclipseEclipse同步选中效果 2013-08-18 11:27 1760
- 01刷LeetCode
- react-router v4 按需加载
- 定时任务命令
- AIDL使用教程一(传递默认类型数据)
- NekoHTML解析HTML为XML后TagName一直为大写的问题解决
- 线性表的链式存储(LinkList)
- Java面试题全集(中)
- 在Linux下使用rsync的6个实例
- JAVA解析JSON数据
- 学生管理系统(四)---查询窗体
- 对线性回归、逻辑回归、各种回归的概念学习
- 写数据库查询的时候谨慎使用时间函数CURDATE()