Android AIDL 使用详解
来源:互联网 发布:桂正和is知乎 编辑:程序博客网 时间:2024/06/16 16:03
AIDL
AIDL (Android Interface Definition Language),Android接口定义语言,Android提供的IPC (Inter Process Communication,进程间通信)的一种独特实现。
为什么使用AIDL
使用AIDL只有在你允许来自不同应用的客户端跨进程通信访问你的service,并且想要在你的service种处理多线程的时候才是必要的。 如果你不需要执行不同应用之间的IPC并发,你应该通过实现Binder建立你的接口,或者如果你想执行IPC,但是不需要处理多线程。那么使用Messenger实现你的接口。
定义AIDL接口的步骤
1、创建.aidl 文件(Android Studio)
// IMyAidlInterface.aidlpackage com.owen.provider;// Declare any non-default types here with import statementsinterface IMyAidlInterface { String getValue();}
2、实现接口并暴露给客户端
Android SDk工具基于.aidl文件使用java语言生成一个接口 这个接口有一个内部抽象类,叫做Stub,它是继承Binder并且实现你AIDL接口的 你必须继承这个Stub类并且实现这些方法,实现一个service并且覆盖onBind()方法返回你的Stub实现类。
public class UpdateService extends Service { private String aidl = "aidl"; @Nullable @Override public IBinder onBind(Intent intent) { return new MyServiceImp(); } private class MyServiceImp extends IMyAidlInterface.Stub { @Override public String getValue() throws RemoteException { return aidl; } }}
实现步骤 :
1、aidl
interface IMyAidlInterface { String getValue();}
2、建立服务类
public class UpdateService extends Service { private String aidl = "aidl"; @Nullable @Override public IBinder onBind(Intent intent) { return new MyServiceImp(); } private class MyServiceImp extends IMyAidlInterface.Stub { @Override public String getValue() throws RemoteException { return aidl; } }}
3、AndroidManifest中申明Service
<service android:name=".UpdateService"> <intent-filter> <!-- 指定调用AIDL服务的ID --> <action android:name="com.owen.provider.IMyAidlInterface" /> </intent-filter></service>
4、启动Service
public class MainActivity extends AppCompatActivity { private IMyAidlInterface iMyAidlInterface; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bindService(new Intent("com.owen.provider.IMyAidlInterface"), new Connection(), Context.BIND_AUTO_CREATE); findViewById(R.id.aidl).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { if (iMyAidlInterface != null) { Toast.makeText(MainActivity.this, iMyAidlInterface.getValue(), Toast.LENGTH_LONG).show(); } } catch (RemoteException e) { e.printStackTrace(); } } }); } private class Connection implements ServiceConnection { @Override public void onServiceConnected(ComponentName name, IBinder service) { iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service); } @Override public void onServiceDisconnected(ComponentName name) { } }}
阅读全文
1 0
- Android AIDL使用详解
- Android AIDL使用详解
- Android AIDL使用详解
- Android AIDL使用详解
- Android AIDL使用详解
- Android AIDL使用详解
- Android AIDL使用详解
- Android AIDL使用详解
- Android AIDL使用详解
- Android AIDL使用详解
- Android AIDL使用详解 .
- Android AIDL使用详解
- Android AIDL使用详解
- Android AIDL使用详解
- Android AIDL使用详解
- Android AIDL使用详解
- Android AIDL使用详解
- android aidl 使用详解
- iOS贝塞尔bezier曲线
- 软件工程之软件开发方法、软件工具和开发环境
- MyBeatis实现数据库的增删改查(CRUD)
- POJ 3279
- C.Primer.Plus(第六版)第17章 编程练习
- Android AIDL 使用详解
- 实现一个列表只需一行代码-RecyclerView适配器暴力封装
- 水经微图功能简介
- kylin使用中曾遇到的问题整理
- SEO优化知识心得
- jsp前端页面调试快捷键
- springboot jar 后台运行 sh
- POJ 1088
- 让你的代码量减少3倍!使用kotlin开发Android(一) 创建Kotlin工程