安卓aidl的详细讲解(终极讲解)
来源:互联网 发布:蒙特卡洛算法量化 编辑:程序博客网 时间:2024/06/08 02:51
1,aidl以前只是知道这个东西,就是没用过,今天到用的时候了,终于把他搞定,开始:
先上服务端的工程结构图:
客户端的工程结构图:
大家看到我两个工程都有相同的aidl文件,且包名相同;
2,上aidl的代码,很简单的代码,一个返回字符串的抽象方法:
package com.myzxing;// Declare any non-default types here with import statementsinterface MyAidlInterface { /** * Demonstrates some basic types that you can use as parameters * and return values in AIDL. */ String order();}
3,上服务端的服务的代码,重要的是在onBind方法里,对aidlInterface的对象返回,这样这个服务就和aidl关联起来了。
public class MyAidlService extends Service { public MyAidlService() { } @Override public void onRebind(Intent intent) { super.onRebind(intent); System.out.println("--- :onRebind"); } @Override public void onCreate() { super.onCreate(); System.out.println("--- :onCreate"); } @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); System.out.println("--- :start serve"); } @Override public IBinder onBind(Intent intent) { System.out.println("--- :IBinder serve"); return aidlInterface; } private final MyAidlInterface.Stub aidlInterface=new MyAidlInterface.Stub() { @Override public String order() throws RemoteException { return "开启wifi"; } };}
服务的清单注册:
<service android:name="com.service.MyAidlService" android:enabled="true" android:exported="true" android:process=":remote"> <intent-filter> <action android:name="com.myzxing.MyAidlInterface"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter></service>
4,上客户端的activity的代码:
public class MainActivity extends AppCompatActivity {TextView textView; //显示从服务端获取的指令 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView= ((TextView) findViewById(R.id.textview)); } //绑定远程服务 public void click(View view){ Bundle args = new Bundle(); Intent intent = new Intent("com.myzxing.MyAidlInterface");//com.myzxing.MyAidlInterface是服务端的服务的action(也是aidl的完整类名) intent.putExtras(args); bindService(intent, mConnection, Context.BIND_AUTO_CREATE); } //获取远程服务的指令 public void click1(View view) throws RemoteException { String order = mService.order(); textView.setText(order); } MyAidlInterface mService; private ServiceConnection mConnection = new ServiceConnection(){ public void onServiceConnected(ComponentName className, IBinder service){ // Log("connect service"); mService = MyAidlInterface.Stub.asInterface(service); } public void onServiceDisconnected(ComponentName className){ mService = null; } };}
5,大家看到我服务端的服务的,这个代码,就是给客户端的信息;测试结果如下:
private final MyAidlInterface.Stub aidlInterface=new MyAidlInterface.Stub() { @Override public String order() throws RemoteException { return "开启wifi"; } };
点击绑定服务,然后点击得到结果,展示如下:
点击绑定服务,然后点击得到结果,展示如下:
6,到此,aidl'已经详细讲解完毕,希望对大家有用》》》》》》
0 0
- 安卓aidl的详细讲解(终极讲解)
- 安卓SIM卡的详细讲解
- AIDL的实例讲解
- 安卓初学者布局详细讲解
- AIDL 讲解
- 安卓Animation讲解
- 安卓LayoutParams讲解
- 安卓音频 讲解
- prototype的详细讲解
- AJAX的详细讲解
- static的详细讲解
- UIScrollView的详细讲解
- UIImageView的详细讲解
- UIScrollView的详细讲解
- UIScrollView的详细讲解
- UITableView的详细讲解
- digester的详细讲解
- UIImageView的详细讲解
- jsp 文件上传下载
- jsp页显示时间标签JSTL标签 <fmt:formatDate/> 实例大全
- 科学家通过解码意念让四肢瘫痪患者弹起吉他
- Leetcode - Word Pattern
- 关于apache-commons-fileupload 的一个异常
- 安卓aidl的详细讲解(终极讲解)
- 指针和引用的区别
- 如何实现XA式、非XA式Spring分布式事务
- 【HUSTOJ】1051: 字符图形7-星号菱形
- web项目设置session监听器,统计同时在线人数
- IO通信模型和Netty 下篇
- 练习正则表达式与Date类的日期变字符串和字符串变日期
- 数学公式画图,人们为什么会被这些画面吸引
- Java 设计模式之策略模式