Android中AIDL的使用的案例
来源:互联网 发布:华为网络安全工程师 编辑:程序博客网 时间:2024/04/28 14:33
本案例需要两个应用 , 一个远程提供服务 , 一个本地调用
//aidl_remoteservice远程
package jacky.aidl_remoteservice;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.support.annotation.Nullable;/** * 作者:Jacky * 邮箱:550997728@qq.com * 时间:2016/2/15 15:56 *///设置服务public class RemoteService extends Service { @Nullable @Override public IBinder onBind(Intent intent) { return new MyBinder(); } public void methodService(){ System.out.println("我是远程服务里面的方法!"); } private class MyBinder extends IService.Stub { public void callMethodService(){ methodService(); } }}
//aidl_localservice本地
package jacky.aidl_localservice;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.os.RemoteException;import android.support.v7.app.AppCompatActivity;import android.view.View;import jacky.aidl_remoteservice.IService;public class MainActivity extends AppCompatActivity { private IService iservice; private MyConn conn=new MyConn(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent = new Intent(); //配置意图,与jacky.aidl_remoteservice中定义的意图要相同 intent.setAction("jacky.aidl_remoteservice"); bindService(intent,conn,BIND_AUTO_CREATE); } public void click(View v){ try { iservice.callMethodService(); } catch (RemoteException e) { e.printStackTrace(); } } //拿到iservice中间人对象(Binder) public class MyConn implements ServiceConnection { @Override public void onServiceConnected(ComponentName name, IBinder service) { //需要使用IService.Stub iservice=IService.Stub.asInterface(service); } @Override public void onServiceDisconnected(ComponentName name) { } }}
运行效果:
点击button , 调用远程服务 , 打印log
在AS中使用AIDL的办法:
[1]右键添加AIDL
[2]在添加的AIDL中实现
0 0
- Android中AIDL的使用的案例
- android中aidl的使用
- android中 AIDL的使用
- Android中AIDL的使用
- Android中AIDL的使用
- android中aidl的简单使用
- Android Studio中AIDL的使用详解
- Android中aidl的解析和使用
- Android中AIDL的实现使用
- Android 中AIDL的使用与理解
- Android studio中AIDL的使用
- Android中AIDL的使用介绍
- Android 中AIDL的使用与理解
- Android中AIDL的使用(一)
- Android中AIDL的使用(二)
- Android 中AIDL的使用与理解
- Android中AIDL的使用详解
- android aidl的使用
- 设计模式 状态模式 以自动售货机为例
- HDU 5036 (STL之bitset)
- POJ_3281_Dining
- struts2拦截器的理解
- 第一行代码笔记之五数据持久化
- Android中AIDL的使用的案例
- Jquery前端封装--DOM加载
- 关于dubbo的理解
- c# 关键字学习_2 sealed
- MarkDown简介
- webservice实例
- mongodb分片环境搭建启动mongos报错
- iOS开发之UIViewController
- hdu1466 dp+直线交点问题