做了一个小例子:
TestAIDLServer.apk是AIDL文件的服务端 TestAIDLProxy.apk是客户端
代码如下
TestAIDLServer中
package com.test.aidl;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
public class MyService extends Service {
public class MyServiceImpl extends IMyService.Stub {
public String getValue() throws RemoteException {
return "Android 链接成功";
}
}
public IBinder onBind(Intent intent) {
return new MyServiceImpl();
}
}
IMyService.aidl中
package com.test.aidl;
interface IMyService
{
String getValue();
}
AndroidManifest.xml中
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.aidl" android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".TestAIDLServer" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyService">
<intent-filter>
<action android:name="com.test.aidl.IMyService" />
</intent-filter>
</service>
</application>
</manifest>
TestAIDLProxy.apk中
吧TestAIDLServer中自动生成的IMyService.java连同包名一起考过来!
TestAIDLProxy.java
package com.test.proxy;
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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import com.test.aidl.IMyService;
public class TestAIDLProxy extends Activity implements OnClickListener {
private IMyService myService = null;
private Button mButton1;
private Button mButton2;
private TextView textView;
private ServiceConnection serviceConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName name, IBinder service) {
// 获得服务对象
myService = IMyService.Stub.asInterface(service);
mButton2.setEnabled(true);
}
public void onServiceDisconnected(ComponentName name) {
}
};
public void onClick(View view) {
switch (view.getId()) {
case R.id.button01:
textView.setText(":-)");
// 绑定AIDL服务
bindService(new Intent("com.test.aidl.IMyService"),
serviceConnection, Context.BIND_AUTO_CREATE);
break;
case R.id.button02:
try {
textView.setText(myService.getValue()); // 调用服务端的getValue方法
} catch (Exception e) {
}
break;
}
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton1 = (Button) findViewById(R.id.button01);
mButton1.setOnClickListener(this);
mButton2 = (Button) findViewById(R.id.button02);
mButton2.setOnClickListener(this);
mButton2.setEnabled(false);
textView = (TextView) findViewById(R.id.mTextView01);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello" />
<Button android:text="@string/buttonstr1" android:id="@+id/button01"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="@string/buttonstr2" android:id="@+id/button02"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<TextView android:layout_width="fill_parent" android:id="@+id/mTextView01"
android:layout_height="wrap_content" android:text="@string/textviewstr" />
</LinearLayout>
分享到:
相关推荐
Android下Aidl的使用,详细请参见博客:http://www.cnblogs.com/plokmju/p/android_Service_aidl_NormalType.html 和 http://www.cnblogs.com/plokmju/p/android_Service_aidl_CustomType.html
本示例代码包括Server.zip 和 Client.zip不仅说明了AIDL的使用而且也介绍了如何传递对象。
这是我博客附带的工程,关于Android service aidl开发的
Android AIDL示例代码, 包含了各种示例、解释等等,值得下载。
android studio 编译aidl以及示例demo。eclipse版本博客:http://blog.csdn.net/qq_16064871/article/details/51385691 我的博客地址:http://blog.csdn.net/qq_16064871
AIDL的示例以及自己简单实现一个AIDL ,code具有一定的参考意义,让人理解AIDL是怎么完成远程调用的
Android使用AIDL完成自定义对象的远程传递的完整代码示例。
演示Android AIDL是如何进行IPC通信
简单的aidl入门学习demo,大家可以通过这个简单的aidl demo学习到怎么入门使用
Android项目之AidlDemo(简单的aidl的例子)。AIDL是Android中IPC(Inter-Process Communication)方式中的一种,AIDL是Android Interface definition language的缩写。需要的小伙伴自请下载。
aidl简单示例源码,对应博客地址:http://blog.csdn.net/chengjun77/article/details/50119913
很简单的Aidl开发的示例,实现步骤的链接见压缩包里的txt说明文件。
这是一个android的aidl例子,里面包含两个项目.LOL游戏和阿里的支付系统.aidl主要是解决跨应用交互问题.
android aidl service
Android使用AIDL实现进程间通信
aidl文件的创建,service文件的创建,aidl通信的演示,服务的绑定
跨进程访问 aidl 客户端示例 本示例与 ch08_aidl 工程配合使用,运行时先运行ch08_aidl服务端,再运行本工程
aidl安卓源码的解析
Android中AIDL的使用Demo。
android通过aidl传递Parcelable对象