1.首先创建一个AIDL文件,并添加上两个接口。
IMyAidlInterface.aidl

package com.example.broadcastdemo;

// Declare any non-default types here with import statements

interface IMyAidlInterface {

void request();
String getName();
}


2.编写服务端实现AIDL相应的接口
MyService.java

public class MyService extends Service {
public MyService() {
}

private final IBinder iBinder = new IMyAidlInterface.Stub() {
@Override
public void request() throws RemoteException {

}

@Override
public String getName() throws RemoteException {
return "您好,我的名字叫服务端";
}
};

@Override
public void onCreate() {
super.onCreate();
}

@Override
public IBinder onBind(Intent intent) {
return iBinder;
}

}

3:使用

private void binAidl() {
if (myAidlInterface == null) {
try {
Intent intent = new Intent("com.example.broadcastdemo.MyService");
intent.setPackage("com.example.broadcastdemo");
boolean result = bindService(intent, ServiceConnection, Context.BIND_AUTO_CREATE);
Log.e(TAG, "createPopupServiceConnection: result " + result);
}catch (Exception e){
Log.e(TAG, "createPopupServiceConnection: Exception" + e.getLocalizedMessage());
}

}
}

private android.content.ServiceConnection ServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.e(TAG, "onServiceConnected: ServiceConnection");
myAidlInterface = IMyAidlInterface.Stub.asInterface(service);
}

@Override
public void onServiceDisconnected(ComponentName name) {
Log.e(TAG, "onServiceDisconnected: ServiceConnection");
myAidlInterface = null;
}
};

private void getName(){
if (myAidlInterface != null) {
try {
String str = myAidlInterface.getName();
Log.e(TAG, "" + str);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}

最新文章

  1. Jquery Md5加密解密
  2. Nginx变量的实现机制
  3. 华为OJ平台——查找组成一个偶数最接近的两个素数
  4. debian下编译libev库
  5. spring中间scope详细解释
  6. linux 原生系统发送电子邮件 (在本地与因特网)
  7. Sublime Text3下如何快速搭建开发环境
  8. ImageLoader的Jar包加载图片
  9. 修改cms版权等等信息
  10. 团队作业7——第二次项目冲刺(Beta版本计划及安排)
  11. SpringBoot: 配置加载顺序
  12. Record && Limit
  13. 上传文件,不依赖 Jquery flash 插件,用到HTML5 input 新属性实现过滤文件格式、同时上传多个文件
  14. 13.1 dubbo服务降级源码解析
  15. flask + mysql写的简单监控系统
  16. UGUI之Canvas和EventSystem
  17. Python数据分析开发环境
  18. php curl使用ss代理
  19. 通过Scanner从控制台获取数据
  20. phpMailer在CentOS 6.5下无法发送邮件的解决办法

热门文章

  1. 在Delphi中高效执行JS代码
  2. Unmount and run xfs_repair
  3. WLAN-无线路由综合应用
  4. ICMP 协议仿真及ping命令用途
  5. 【Go语言学习笔记】Go的defer
  6. springboot利用mock进行junit单元测试,测试controller
  7. ABP开发框架中分页查询排序的实现处理
  8. celery ValueError: invalid literal for int() with base 10: '26379;sentinel'
  9. Flume面试题整理
  10. Part 20 Create custom service in AngularJS