package com.example.aidlservice;
interface ICat {
String getColor();
double getWeigth();
}
public class AidlService extends Service {
private String color;
private double weight;
private CatBinder catBinder;
Timer timer = new Timer();
String[] colors = new String[]{"红色","白色","黑色"};
@Override
public IBinder onBind(Intent arg0) {
return catBinder;
}
public class CatBinder extends Stub{
@Override
public String getColor() throws RemoteException {
return color;
}
@Override
public double getWeigth() throws RemoteException {
return weight;
}
}
@Override
public void onCreate() {
super.onCreate();
catBinder = new CatBinder();
timer.schedule(new TimerTask() {
@Override
public void run() {
int rand = (int)(Math.random()*3);
color = colors[rand];
weight = Math.random()*100;
}
}, 0, 800);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
timer.cancel();
}
}
<service android:name=".AidlService">
<intent-filter >
<action android:name="com.example.aidlservice.AIDL_SERVICE"/>
</intent-filter>
</service>
这样,远程调用的servcie就定义好了,在开发工具中,是单独的一个项目哦,然后生成apk,安装。
- Activity调用,要新建一个工程,然后将aidl文件复制到新的项目中。
- activity代码:
public class AIDLActivity extends Activity {
ICat catService;
TextView tView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activitylist);
Button button4 = (Button) this.findViewById(R.id.activitylistbutton4);
tView = (TextView) this.findViewById(R.id.tvw);
Intent intent = new Intent();
intent.setAction("com.example.aidlservice.AIDL_SERVICE");
bindService(intent, connection, Service.BIND_AUTO_CREATE);
button4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
tView.setText(catService.getColor()+"----"+catService.getWeigth());
} catch (RemoteException e) {
e.printStackTrace();
}
Toast.makeText(AIDLActivity.this, "服务已经启动", Toast.LENGTH_LONG).show();
}
});
}
@Override
protected void onStart() {
super.onStart();
}
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
System.err.println("service is unconnected");
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//System.err.println(ICat==null);
catService = ICat.Stub.asInterface(service) ;
System.err.println("service is connected");
}
};
protected void onDestroy() {
super.onDestroy();
unbindService(connection);
};
}
分享到:
相关推荐
1、后台服务里是一个Android项目,是一个Service 后台服务 ...中通过AIDL获取简单数据与复杂数据 导入,分别运行(先运行后台服务,没有界面),在运行前台界面(先绑定,再点击中间按钮,最后点击解除绑定)
AIDL远程调用,传递基础数据类型
AIDL简单使用示例代码,关于如何使用AIDL https://blog.csdn.net/u013293125/article/details/77368261
aidl简单示例源码,对应博客地址:http://blog.csdn.net/chengjun77/article/details/50119913
经典企业级AIDL进程间传输复杂数据的例子
传递复杂数据的AIDL服务 服务端的示例程序,这个是服务器端,与ch08_complextypeaidlclient工程配合使用
默认情况下,AIDL只支持下列数据类型: Java八种基础数据类型(如 int、long、char、boolean 等); String字符串; CharSequence字符序列; List列表,List中的所有元素须是前面提到的数据类型,或者是Parcelable...
AIDL的示例以及自己简单实现一个AIDL ,code具有一定的参考意义,让人理解AIDL是怎么完成远程调用的
AIDL简单的示例代码,主要是关于AIDL如何使用。https://blog.csdn.net/u013293125/article/details/77368261
很简单的Aidl开发的示例,实现步骤的链接见压缩包里的txt说明文件。
Android使用AIDL完成简单的远程加法计算的完整示例代码。
简单的aidl示例,快速理解apk之间的进程通讯
Android AIDL传输对象代码 Parcelable 在AIDL client的Activity中点击传输对象按钮查看日志,就会看到对象里面的内容
aidl 简单demo 在两个应用间传输笔迹 包含aidl的简单运用和画布的简单实现
android aidl activity 调用部分。
传递复杂数据的AIDL服务 客户端的示例程序,与ch08_complextypeaidl配合使用
Aidl传递 复杂数据 基本代码事例
Android下Aidl的使用,详细请参见博客:http://www.cnblogs.com/plokmju/p/android_Service_aidl_NormalType.html 和 http://www.cnblogs.com/plokmju/p/android_Service_aidl_CustomType.html
aidl 简单demo 在两个应用间传输笔迹 包含aidl的简单运用和画布的简单实现
AIDL的简单例子,一看就懂!