Android进程通信:AIDL入门实例
来源:互联网 发布:土行孙软件 编辑:程序博客网 时间:2024/05/17 07:56
AIDL即 Android Interface Definition Language。原因:On Android, one process cannot normally access thememory of another process.
也就是说AIDL用于android进程间通信,下面就记录一下第一个aidl的demo。
官方文档也给出了基本的使用方法,如下图:
1. 在android项目的src相关包下创建一个aidl文件 IRemoteService.aidl:
package com.example.testndk;interface IRemoteService { /** Request the process ID of this service, to do evil things with it. */ int getPid(); /** Demonstrates some basic types that you can use as parameters * and return values in AIDL. */ void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString);}创建成功后会在gen目录自动生成一个同名的java文件,打开可以看到其实是一个接口
public interface IRemoteService extends android.os.IInterface
2. 实现这个接口
package com.example.testndk;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.os.Process;public class RemoteService extends Service { @Override public void onCreate() { super.onCreate(); } @Override public IBinder onBind(Intent intent) { // Return the interface return mBinder; } private final IRemoteService.Stub mBinder = new IRemoteService.Stub() { public int getPid(){ return Process.myPid(); } public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) { // Does nothing } };}
3. 把接口暴露给客户端,即调用处
package com.example.testndk;import android.app.Activity;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.os.RemoteException;import android.util.Log;import android.widget.TextView;public class TestNdkActivity extends Activity { private static final String TAG = "TestNdkActivity"; private TextView tv1 = null; // static { // System.loadLibrary("TestNdk"); // } /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // // String str = JniClient.AddStr("prefix", "suffix"); // // // int iSum = JniClient.AddInt(5, 2); // String strSum = "5 + 2 = " + iSum; tv1 = new TextView(this); bindService(new Intent(RemoteService.class.getName()), mConnection, Context.BIND_AUTO_CREATE); setContentView(tv1); } IRemoteService mIRemoteService = null; private ServiceConnection mConnection = new ServiceConnection() { // Called when the connection with the service is established public void onServiceConnected(ComponentName className, IBinder service) { // Following the example above for an AIDL interface, // this gets an instance of the IRemoteInterface, which we can use // to call on the service mIRemoteService = IRemoteService.Stub.asInterface(service); try { tv1.setText("mIRemoteService=" + mIRemoteService.getPid()); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // Called when the connection with the service disconnects unexpectedly public void onServiceDisconnected(ComponentName className) { Log.e(TAG, "Service has unexpectedly disconnected"); mIRemoteService = null; } }; protected void onDestroy() { super.onDestroy(); unbindService(mConnection); }}
用到了service,还需在AndroidManifest.xml配置一下,添加
<service android:name="com.example.testndk.RemoteService"> <intent-filter> <action android:name="com.example.testndk.RemoteService"/> </intent-filter> </service>
因为是通过action来查找service的,所以action标签不可少。
官方文档的例子稍微麻烦一点,不过也是比较清楚的。
0 0
- Android进程通信:AIDL入门实例
- AIDL实现Android进程间通信实例
- 进程通信aidl入门
- android 进程通信aidl
- android 进程通信--aidl
- android使用aidl实现进程间通信的实例
- Android Studio使用AIDL 实现进程间通信实例
- Android入门:通过AIDL进行进程间通信
- Android入门:通过AIDL进行进程间通信
- Android 进程间通信(IPC)---AIDL入门
- Android 进程通信之AIDL
- android AIDL 进程间通信
- Android AIDL进程通信 记录
- android aidl进程间通信
- Android 夸进程通信 AIDL
- Android进程通信之AIDL
- Android 进程通信之AIDL
- Android 进程间通信AIDL
- 详解Spring事件驱动模型
- 理解 Android Build 系统_Make 文件说明_4
- 关于Java的若干基础知识
- 不用USBASP芯片也可用USB,纯AVR实现USB通讯:AVRUSB
- Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED
- Android进程通信:AIDL入门实例
- 理解 Android Build 系统_Make 目标说明_5
- IOC---教你打造 Android 中的 IOC 框架【ViewInject】 (下)
- ios开发UI单视图控制器
- JavaScript语法规范学习笔记
- iOS集成支付宝
- Excel 2007 打开 UTF-8 编码 CSV 文件的BUG
- ccflow 流程设计器“经典模式”运行,在途列表里的“撤销发送”不好用
- poi 创建Excel