[置顶] Android中ail的作用和使用说明
16lz
2021-01-26
为什么要使用aidl
Android系统中的进程之间不能共享内存,为了使其他的应用程序也可以访问本应用程序提供的服务的远程调用。这么说有有些太官方了,简单的理解就是新浪微博的SSO授权快速登录。你本地有Cilent和Service两个应用,使用aidl之后Client应用可以直接调用Service应用中的方法了。
使用注意
本地创建cilent和service两个应用,aidl文件必须要同包、同名、同方法。
Demo下载地址
http://download.csdn.net/detail/aaren_jiang/7494661
共有的.aidl文件
package com.chiang.aidl;interface RemoteSSO {String getPackageName();String getActivityName(String param);}
Service端
这里是JAVA代码public class AIDLService extends Service {private static final String TAG = "AIDLService";private void Log(String str) {Log.i(TAG, "----------" + str + "----------");} public void onCreate() {Log("service created");}public void onStart(Intent intent, int startId) {Log("service started id = " + startId);}public IBinder onBind(Intent t) {Log("service on bind");return mBinder;}public void onDestroy() {Log("service on destroy");super.onDestroy();}public boolean onUnbind(Intent intent) {Log("service on unbind");return super.onUnbind(intent);}public void onRebind(Intent intent) {Log("service on rebind");super.onRebind(intent);}/** * Service实现该方法,给远程Client调用 */private final RemoteSSO.Stub mBinder = new RemoteSSO.Stub() {@Overridepublic String getPackageName() throws RemoteException {Log.e(TAG, "remote call from client! current thread id = " + Thread.currentThread().getId());return "调用service获取包名";}@Overridepublic String getActivityName(String param) throws RemoteException {Log.e(TAG, "remote call from client! current thread id = " + Thread.currentThread().getId() +" param:"+param);return "调用service获取activity名";}};}AndroidMainifest.xml文件的配置,只需要在application标签里增加service标签即可
<service android:name=".AIDLService" android:exported="true" > <intent-filter> <!-- 这里的action是给cilent调用的标识 --> <action android:name="com.chiang.aidlservice.aidl" /> </intent-filter> </service>
Clinet端
/** * 注意.aidl文件要和被调用的.aidl文件要是一样的包名、类名、方法名,否则你调用会错误 * @author JiangHao * */public class MainActivity extends Activity implements OnClickListener {public static final String TAG = "AIDL";private RemoteSSO remoteSSO;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//根据service的action名称来调用Intent intent = new Intent("com.chiang.aidlservice.aidl");bindService(intent, connection, Context.BIND_AUTO_CREATE);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.callRemote:{//远程调用if(remoteSSO==null){Toast.makeText(this, "等待连接...", Toast.LENGTH_SHORT).show();return;}try {Toast.makeText(this, "---"+remoteSSO.getActivityName("我去")+"---", Toast.LENGTH_SHORT).show();} catch (RemoteException e) {e.printStackTrace();Toast.makeText(this, "---错误---", Toast.LENGTH_SHORT).show();}break;}case R.id.cancalCall:{//注销unbindService(connection);break;}}}ServiceConnection connection=new ServiceConnection() {@Overridepublic void onServiceDisconnected(ComponentName name) {Log.i(TAG, "注销连接!");remoteSSO = null;}@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {Log.i(TAG, "连接成功!");remoteSSO = RemoteSSO.Stub.asInterface(service);}};}
更多相关文章
- 戏说Android(安卓)view 工作流程《上》
- 用 天天模拟器 + Eclipse或Android(安卓)Studio调试app
- flutter项目的打包和发布 主要是Android
- 2.Eclipse版本android 65535解决方案(原理等同android studio现
- Android中关于JNI 的学习(零)简单的例子,简单地入门
- 为何 Android(安卓)4手机不能当 U 盘了
- Android(安卓)面试题(有详细答案)
- 点击事件分发机制 关键源码笔记
- Android(安卓)JAR组件安装打包教程