android之了解AIDL
来源:互联网 发布:淘宝代刷自动返款系统 编辑:程序博客网 时间:2024/06/05 08:46
1、什么是AIDL? Android Interface Definition Lauguage(android接口描述语言)是一个IDL语言。 2、AIDL的作用? 背景:在android平台 中,一个进程通常不能访问其它进程中的内存区域。所以,他们需要把对象拆分成操作系统能理解的简单形式,以便伪装成对象跨越边界访问。编写这种伪装代码相当的枯燥乏味,好在android为我们提供了AIDL工具可以来做这件事 作用:用来进行进程间通信,有很多人可能就会问到,进程间通信有很多方法,为什么非要用AIDL了?进程间通信的确有很多,如广播、Message,Content Provider。这些是可以进行进程间通信,但是广播是单向,Message只能在同一个进程通信,而不能跨进程通信,Content Provider不是实时的。而AIDL拥有这他们所没有的优点,双向和跨进程通信、实时。 那在什么情况下使用AIDL了? 官方文档介绍: Note: Using AIDL is necessary only if you allow clients from different applications to access your service for IPC(Inter-Process Communication,进程间通信)I and want to handle multithreading(多线程) in your service. If you do not need to perform concurrent(并发) IPC across different applications, you should create your interface by implementing a Binder or, if you want to perform IPC, but do not need to handle multithreading, implement your interface using a Messenger. Regardless, be sure that you understand Bound Services before implementing an AIDL. 翻译过来的大概意思就是:允许来自于不同的客户应用程序访问你的服务器并且处理多线程问题时你才必须使用AIDL 3、使用流程 要使用AIDL,Service需要以aidl文件的方式提供服务接口,AIDL工具将生成一个相应的java接口,并且在生成的服务接口中包含一个功能调用的stub服务桩类。Service的实现类需要去继承这个stub服务桩类。Service的onBind方法会返回实现类的对象,之后你就可以使用它了 4、小程序应用 首先创建一个后缀名为aidl的文件,我的为Manager.aidl package com.test.service.aidl;interface Manager{ float add(float num1,float num2);} 写一个实现它的类ManagerImpl.javapackage com.test.service.aidl;public class ManagerImpl extends Manager.Stub{ @Override public float add(float num1,float num2){ return num1 + num2; }} 写一个服务类MyService.javapackage com.test;import com.test.service.aidl.Manager;import com.test.service.aidl.ManagerImpl;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.util.Log;public class MyService extends Service{ private Manager.Stub binder; public void onCreate(){ super.onCreate(); binder = new ManagerImpl(); System.out.println("---->onCreate"); Log.e("--->", "---->onCreate"); } public int onStartCommand(Intent intent, int flags, int startId) { System.out.println("---->onStart"); return super.onStartCommand(intent, flags, startId); } //外界要想访问服务,是通过返回的binder访问的 @Override public IBinder onBind(Intent arg0) { return binder; }} 在AndroidMainfest.xml配置资源 android:process=":push"是另外一个进程<service android:name=".MyService" android:enabled="true" android:exported="true" android:process=":push" > </service>最后就写测试类了MainActivity.javapackage com.test.ui.activity;import com.test.service.aidl.Manager;import com.test.MyService;import com.test.R;import com.test.service.aidl.ManagerImpl;import android.os.Bundle;import android.os.IBinder;import android.os.RemoteException;import android.view.View;import android.widget.EditText;import android.widget.TextView;import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;public class MainActivity extends Activity { private Manager binder; private EditText etNum1,etNum2; private TextView tvResult; private ServiceConnection serviceConnect = new XmppServiceConnect(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); System.out.println("---->onCreate1"); init(); } protected void onStart() { super.onStart(); //活动开始时绑定MyService,就会自动调用serviceConnect bindService(new Intent(this, MyService.class), serviceConnect, BIND_AUTO_CREATE); } private void init() { etNum1 = (EditText)findViewById(R.id.etNum1); etNum2 = (EditText)findViewById(R.id.etNum2); tvResult = (TextView)findViewById(R.id.tvResult); } protected void onDestroy() { super.onDestroy(); unbindService(serviceConnect); } public void btn_add_click(View v) throws RemoteException{ float num2 = Float.parseFloat(etNum2.getText().toString()); float num1 = Float.parseFloat(etNum1.getText().toString()); tvResult.setText(binder.add(num1, num2)+""); } private class XmppServiceConnect implements ServiceConnection { //开始绑定服务时就执行下面的方法 public void onServiceConnected(ComponentName componentName, IBinder iBinder) { binder = Manager.Stub.asInterface(iBinder);//特别注意这里iBinder是服务那边传过来的 } //解绑时才调用 public void onServiceDisconnected(ComponentName componentName) { binder = null; } }}界面就很简单了,两个EditText,一个TextView和一个Button恩结束了,如果有错的地方,还希望有人指出来
0 0
- android之了解AIDL
- 了解Android的Aidl机制
- Android Service 之 AIDL
- Android之AIDL
- android 之 AIDL
- Android 之工具aidl
- Android之AIDL
- Android之AIDL使用
- Android之AIDL
- Android服务之AIDL
- Android IPC之AIDL
- Android IPC 之AIDL
- Android之AIDL
- Android之AIDL服务
- Android IPC之AIDL
- Android之AIDL
- Android之AIDL
- Android 之工具aidl
- android之多进程下Application.getSharedPreferences的取值
- 如何写一个拼写检查器—Peter Norvig 中文翻译:徐宥
- ntpdate刷新linux系统时间
- c语言内存操作函数
- Android学习第一天
- android之了解AIDL
- CDN的原理
- 第五届蓝桥杯B组java—奇怪的算式
- 机器学习-朴素贝叶斯分类
- TestWidget.cpp_2
- 蓝桥杯:求一个5位数的最大循环素数和 回文素数
- java之观察者模式
- 4月4号 机房训练赛(数据结构)
- TestWidget.h_2