android 绑定远程服务
来源:互联网 发布:淘宝hd 5.0.1 ios 编辑:程序博客网 时间:2024/05/16 01:10
aidl : android interface defination language 安卓接口定义语言.
提供两个应用程序之间交互的接口
IPC: inter process communication 进程间通讯
首先创建一个远程服务的程序,创建一个Service,在程序中配置它的action,然后创建aidl接口
Service代码:
public class RemoteSerivce extends Service {@Overridepublic IBinder onBind(Intent intent) {return new MyBinder();}private class MyBinder extends IService.Stub {@Overridepublic void callInServiceMethod() throws RemoteException {serviceMethod();}}private void serviceMethod() {System.out.println("远程服务里面的方法");}}
配置文件的代码:
<service android:name="com.alan.remodeservice.RemoteSerivce" > <intent-filter> <action android:name="aaa.bbb.ccc.remote" /> </intent-filter> </service>
接着创建一个调用远程服务的方法的程序:
activity代码:
public class MainActivity extends Activity {private Button mRemoteButton;private Button mRemoteMethod;private IService iService;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mRemoteButton = (Button) findViewById(R.id.button1);mRemoteMethod = (Button) findViewById(R.id.button2);// 绑定远程服务mRemoteButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent();// 远程配置的action是什么要与之对应intent.setAction("aaa.bbb.ccc.remote");bindService(intent, new ServiceConnections(), BIND_AUTO_CREATE);}});// 调用远程服务的方法mRemoteMethod.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {try {iService.callInServiceMethod();} catch (RemoteException e) {e.printStackTrace();}}});}class ServiceConnections implements ServiceConnection {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {// 这行是重点iService = IService.Stub.asInterface(service);}@Overridepublic void onServiceDisconnected(ComponentName name) {}}}
在这里最重要的是IService.aidl文件,在远程当中的IService.aidl文件的包名与调用远程方法的程序的包名必须要一致,两个程序都必须要有IService.aidl这个文件
IService.aidl代码:
package com.alan.remodeservice; interface IService { void callInServiceMethod();}这里提醒一下,接口和方法都不能声明为public的,会报错。因为这里的接口默认都是全局可见的。
0 0
- Android远程服务绑定
- android 绑定远程服务
- android,aidl,绑定远程服务,调用远程服务的方法
- 绑定服务&绑定远程服务
- Android 采用AIDL绑定远程服务
- android 的aidl绑定远程服务
- android(37)(绑定远程服务)
- Android使用aidl来绑定远程服务
- Android Service(服务)绑定远程服务一个小demo
- 绑定远程服务
- 远程服务的绑定
- AIDL绑定远程服务
- aidl绑定远程服务
- android绑定远程服务以及android接口定义语言(aidl)
- android开发之------绑定远程服务的流程aidl
- Android:Service(三)——Aidl绑定远程服务
- 24_aidl绑定远程服务
- 采用aidl绑定远程服务
- 计算机科学中最重要的32个算法
- Cocos2d-x3.0 Json解析
- 作业8
- Linux中strchr与strstr函数实现。
- CSDN第一天
- android 绑定远程服务
- 第五周作业
- HTM5之WEB Worker
- linux平台下 延迟工作队列实例
- 行为型模式3:迭代器模式
- mysql学习 常用语句1
- 职员有薪水了
- Android 中文 API (101) —— AsyncTask
- Drupal 7 Module Development 试译笔记 - 第三章