IDEA创建AIDL示例
来源:互联网 发布:python 股票分析 教程 编辑:程序博客网 时间:2024/06/15 03:51
在网上看了好几篇关于AIDL的入门的文章终于写出了我的第一个AIDL入门例子,特此发帖
—— 每个程序员都有一颗开源的心
服务端:
1.服务端创建aidl,build project
package com.fafa.aidlserver;interface RemoteService { String getInfor(String s);}
2.创建service
package com.fafa.aidlserver;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.os.RemoteException;import android.util.Log;/** * Created by ZhouBing on 2017/8/14. */public class MyService extends Service{ private final IBinder binder = new RemoteService.Stub() { @Override public String getInfor(String s) throws RemoteException { return "我是Service返回的字符串"; } }; @Override public IBinder onBind(Intent intent) { Log.d("aidl", "aidlService--------------onBind"); return binder; } @Override public void onCreate() { super.onCreate(); Log.d("aidl", "aidlService--------------onCreate"); }}
3.暴露服务
<service android:name=".MyService"> <intent-filter> <action android:name="com.fafa.aidlserver.MyService"/> </intent-filter> </service>
客户端
1.拷贝aidl自动生成的接口文件到客户端,注意一定要连包一起拷过去,不然出错?
2.绑定服务
package com.fafa.aidlclient;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.IBinder;import android.os.RemoteException;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import com.fafa.aidlserver.RemoteService;public class MainActivity extends AppCompatActivity { public final static String TAG = "aidl"; private RemoteService myInterface; private ServiceConnection serviceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { myInterface = RemoteService.Stub.asInterface(service); Log.i(TAG, "连接Service 成功"); try { String s = myInterface.getInfor("我是Activity传来的字符串"); Log.i(TAG, "从Service得到的字符串:" + s); } catch (RemoteException e) { e.printStackTrace(); } } @Override public void onServiceDisconnected(ComponentName name) { Log.e(TAG, "连接Service失败"); } }; @Override protected void onDestroy() { super.onDestroy(); unbindService(serviceConnection); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent = new Intent(); intent.setAction("com.fafa.aidlserver.MyService"); intent.setPackage("com.fafa.aidlserver"); bindService(intent,serviceConnection,BIND_AUTO_CREATE); }}
安装服务端,运行客户端
这篇博客可以看下 http://blog.csdn.net/wuzhipeng1991/article/details/40376507
代码 传送门
阅读全文
0 0
- IDEA创建AIDL示例
- Android 使用 aidl 文件创建服务示例
- Android 使用 aidl 文件创建服务示例
- idea创建struts项目示例
- AIDL示例
- IntelliJ IDEA 中创建HelloWorld项目示例
- AIDL使用示例
- AIDL示例编程
- Android AIDL示例
- Java开发工具IntelliJ IDEA创建Andriod项目示例说明
- 学习笔记: IDEA+maven创建webapp项目示例
- IntelliJ IDEA 中创建HelloWorld项目示例(非Maven项目)
- IntelliJ IDEA安装scala插件并创建scala示例
- Android AIDL创建Service
- eclipse 创建aidl
- Android创建.aidl文件
- Android:AIDL创建Service
- Android创建AIDL服务
- 数据结构总结
- 调用RadioGroup的check(id)方法,监听onCheckedChanged方法被执行多次;调用clearCheck(),onCheckedChanged方法仍被执行的解决办法
- POJ1833 排列(next_permutation()的应用)
- HDU 6096 String AC自动机(多种解法)
- MariaDB设置数据库默认编码
- IDEA创建AIDL示例
- uva 10129 Play on Words
- ARKit 获取识别到的水平面
- 关于iOS中的线程安全和使用场景
- wait()和notify()
- hdu5926Mr. Frog’s Game
- could-not-read-Username-for-https-github-com-Invalid-argument
- 三星910S3L安装系统修改Boot
- <job_demo><Qt>数据库简单查询