安卓最简单的aidl用两个程序模拟进程通信
来源:互联网 发布:php中的正则表达式 编辑:程序博客网 时间:2024/05/30 04:30
最近要面试了 没 时间玩了.看了下安卓icp ,说实话一直没过aidl这东东 发现网上帖子好多 下载的却好少 .好吧自己玩了一个 留下做个备注吧.
demo下载链接http://download.csdn.net/detail/yung7086/8584683
服务端接受用户在文本框输入文字
客户端调用服务的service得到文字在界面显示
看到网上很多例子都是服务端只有一个service没有界面所以看到效果不好.
整理下步骤
1创建 aidl接口 文件implaidl.aidl 记得后缀 ,注意不要修饰符哦 我的如此的简单 然后gen目录自动身成了一个对应的包下的implaidl.java文件 里面有个抽象的静态内部类Stub 实现了我们的接口继承了binder 我们发现安卓跨进程常用Ibinder接口,这太深奥我不懂 但是发现onbindservice时候connectionservice也会返回一个ibinder接口,这是安卓内部密码把
package com.yung.aidlclient;interface implaidl{ String getText();}
看一下工程目录 很简单明了
先做客户端 包含implaidl.aidl的包是从服务端考过来就是了 所以客户端只有一个类展示获取服务端的数据
package com.yung.aidl;import android.app.Activity;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.os.RemoteException;import android.widget.TextView;import com.yung.aidlclient.implaidl;import com.yung.aidlservice.R;/** * 2015年4月11日 15:54:13 * * @author yung 这是调用者 客户端 */public class ShowServiceAct extends Activity {TextView tv;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.mainact);tv = ((TextView) findViewById(R.id.textview1));}@Overrideprotected void onResume() {super.onResume();// 这个action 是服务端的service在manidest注册定义的action 的namebindService(new Intent("com.yung.aidl"), new ServiceConnection() {@Overridepublic void onServiceDisconnected(ComponentName name) {}@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {// TODO Auto-generated method stubimplaidl i = implaidl.Stub.asInterface(service);try {tv.setText(i.getText());} catch (RemoteException e) {e.printStackTrace();}}}, Context.BIND_AUTO_CREATE);}}
客户端就一个展示服务端的数据 就可以了 OK
在看下服务端吧 服务端 提供一个输入的界面 输入的数据 保存在文件提供给客户端
服务端 提供一个输入的界面 输入的数据 保存在文件提供给客户
package com.yung.aidlclient;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import android.app.Activity;import android.os.Bundle;import android.os.Environment;import android.view.View;import android.view.View.OnClickListener;import android.widget.EditText;import android.widget.Toast;/**服务端 提供一个输入的界面 输入的数据 保存在文件提供给客户端 * @author yung * */public class Main_ShowServiceAct extends Activity implements OnClickListener {public EditText ed;public static String filePath = "aidl.text";@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.mainact);ed = (EditText) findViewById(R.id.editText1);}@Overridepublic void onClick(View v) {saveTextContent(ed.getText().toString() + " 保存时间:"+ System.currentTimeMillis());}private void saveTextContent(String str) {File file = new File(getsdpath() + "/" + filePath);try {if (!file.exists())file.createNewFile();FileOutputStream out = new FileOutputStream(file);out.write(str.getBytes());out.flush();out.close();Toast.makeText(this, "文字保存完毕", Toast.LENGTH_SHORT).show();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public static String getsdpath() {return Environment.getExternalStorageDirectory().getAbsolutePath();}}
最终要的service出来的 这个service在服务端实现了处理 但是在终端调用
package com.yung.aidlclient;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.os.RemoteException;/** * 服务端的服务 提供给客户端调用 * * @author yung 2015年4月11日 16:18:23 */public class AcceptServiceSer extends Service {@Overridepublic IBinder onBind(Intent intent) {return new implaidl.Stub() {@Overridepublic String getText() throws RemoteException {return getTextContent();}};}//由于public String getTextContent() {File file = new File(Main_ShowServiceAct.getsdpath() + "/"+ Main_ShowServiceAct.filePath);FileInputStream read;try {read = new FileInputStream(file);byte[] bys = new byte[read.available()];read.read(bys);return new String(bys);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return null;}}
加上服务断的清单文件
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".Main_ShowServiceAct" > <intent-filter> <action android:name="android.intent.action.MAIN" > </action> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".AcceptServiceSer" > <intent-filter> <action android:name="com.yung.aidl" /> </intent-filter> </service> </application>
没看懂?代码太多 下demo看吧
http://download.csdn.net/detail/yung7086/8584683
1 1
- 安卓最简单的aidl用两个程序模拟进程通信
- 进程间通信Aidl的简单案例
- 进程中简单的AIDL通信实现
- 简单的AIDL通信
- 使用AIDL完成一次简单的Android进程间通信
- Android进程间通信-AIDL的简单使用
- android进程间通信AIDL的简单实现。
- 两个程序的进程间通信小结
- 两个程序的进程间通信小结
- 进程之间的通信AIDL
- aidl进程之间的通信
- android 进程间通信 aidl 简单理解
- 安卓Service组件使用系列6:使用AIDL完成两个进程间的通信
- android 用aidl实现进程间的通信
- 使用AIDL实现进程间的通信
- 使用AIDL实现进程间的通信
- Android:进程间的通信: AIDL
- android的AIDL----讲述进程间通信
- 例题7-12 旋转游戏 UVa1343
- Android自定义View的实现方法,带你一步步深入了解View(四)
- 黑马程序员---OC基础知识⑦
- Python爬虫Csdn系列III
- javascript基础笔记一数据类型
- 安卓最简单的aidl用两个程序模拟进程通信
- Android 定时器的三种实现
- 谈谈JAVA如何计算字符串公式
- 设计模式之代理模式学习二----普通代理----------《设计模式之禅》学习笔记
- cocos2d-x 3.3 之黑白棋设计 NO.1 初步设计
- NGUI控件说明(中文) UICamera
- 函数递归(求字符串的长度)
- 1.12
- 算数表达式求值