调用远程服务
来源:互联网 发布:自组织网络算法 编辑:程序博客网 时间:2024/05/16 12:13
远程服务类
清单文件中注册
<service android:name=".RemoteService"> <intent-filter> <action android:name="com.gjj.remoteservice"/> </intent-filter></service>
package com.example.test;import android.app.Service;import android.content.Intent;import android.os.IBinder;import com.example.test2.IRemoteInterface;/** * Created by joy on 2016/1/12. */public class RemoteService extends Service { @Override public IBinder onBind(Intent intent) { System.out.println("绑定了"); return new Mybind(); } class Mybind extends IRemoteInterface.Stub { @Override public void qianxian() { RemoteService.this.say(); } } public void say(){ System.out.println("我是远程服务"); }}
IRemoteInterface.Stub为创建的AIDL文件自动生成的类
首先创建AIDL文件IRemoteInterface接口,然后在android studio中make module自动生成IRemoteInterface.java,现在可以调用了
注意这里的AIDL文件的包名必须和客户端的一致
客户端
package com.example.test2;import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.os.RemoteException;import android.view.View;public class LocalActivity extends Activity { private IRemoteInterface remoteInterface; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_local); } public void startRemoteService(View v){ Intent intent=new Intent(); intent.setAction("com.gjj.remoteservice"); bindService(intent,new MyServiceConn(),BIND_AUTO_CREATE); } public void callRemoteMethod(View v){ try { remoteInterface.qianxian(); } catch (RemoteException e) { e.printStackTrace(); } } class MyServiceConn implements ServiceConnection{ @Override public void onServiceConnected(ComponentName name, IBinder service) { remoteInterface= IRemoteInterface.Stub.asInterface(service); } @Override public void onServiceDisconnected(ComponentName name) { } }}
0 0
- 远程调用服务
- Hessian-远程服务调用
- aidl远程服务调用
- 调用远程服务
- RPC调用远程服务
- android_58_service_remote_AIDL调用远程服务
- android_59_service_aidl调用远程服务
- jvisualvm 调用远程服务
- Spring的远程服务调用
- 远程服务调用-JMS协议
- python远程调用webservice服务
- java远程服务调用方法
- dubbo 远程服务无法调用
- 采用AIDL调用远程服务
- Java RMI远程服务调用
- dubbo 远程服务调用流程
- JFinal+Hessian调用远程服务
- hessian远程服务调用例子
- UPYUN的云CDN技术架构演进之路
- ArcGIS中QueryTask,FindTask,IndentifyTask 之间的区别
- 介绍几个网站(关于iOS)
- leercode Word Search
- IOS DEMO大全
- 调用远程服务
- Docker入门实战-SSH连接docker容器
- 前台图片样式的相关处理
- LeetCode_OJ【40】Combination Sum II
- nodejs Tips7
- Vim Global Command用法(g命令)
- ajaxFileUpload 怎么传相关参数到后台
- Windows下配置使用Memcached Manager
- HTML与XHTML的区别