- 浏览: 908991 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
itzhongyuan:
java Random类详解 -
david_je:
你好,我看到你在C里面回调JAVA里面的方法是在native里 ...
Android NDK开发(1)----- Java与C互相调用实例详解 -
fykyx521:
请求锁是在 oncreate 释放实在ondestroy?? ...
Android如何保持程序一直运行 -
aduo_vip:
不错,总结得好!
Android读取assets目录下的资源 -
f839903061:
给的网址很给力哦!
Android 4.0.1 源码下载,编译和运行
Android使用binder访问service的方式
- 博客分类:
- android
1. 我们先来看一个与本地service通信的例子。
local servcie 的代码如上,在onBinder方法中返回binder,binder包含了service的句柄,客户端得到句柄以后就可以调用servcie的公共方法了,这种调用方式是最常见的。
需要注意的是在onStop中要解绑定service, 否则会造成内存泄露的问题。
2. 我们再看一下与另外一个进程中的service进行通信的问题(跨进程通信!)。
如何将servcie运行在另外一个进程呢?在manifest 里面配置个属性就行了。
android:process=":remote" , 代表这个service运行在同一个应用程序的不同进程中。
获得service端传来的binder,用来构建一个Messenger向service发送消息。
构建一个Messenger,包含一个handler,然后将messenger的binder传给客户端,客户端可以通过handler再构造一个messenger与service通信,消息在handler里面被处理。
现在是service端单向响应客户端的消息,同理可以做成双向发送消息,实现双向通信。
public class LocalService extends Service { @Override public IBinder onBind(Intent intent) { return new LocalBinder(); } public void sayHelloWorld(){ Toast.makeText(this.getApplicationContext(), "Hello World Local Service!", Toast.LENGTH_SHORT).show(); } public class LocalBinder extends Binder { LocalService getService() { // Return this instance of LocalService so clients can call public methods return LocalService.this; } } }
local servcie 的代码如上,在onBinder方法中返回binder,binder包含了service的句柄,客户端得到句柄以后就可以调用servcie的公共方法了,这种调用方式是最常见的。
public class LocalServiceTestActivity extends Activity { static final String TAG = "LocalBinderTestActivity"; ServiceConnection mSc; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mSc = new ServiceConnection(){ @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.d(TAG, "service connected"); LocalService ss = ((LocalBinder)service).getService(); ss.sayHelloWorld(); } @Override public void onServiceDisconnected(ComponentName name) { Log.d(TAG, "service disconnected"); } }; } @Override protected void onStart() { super.onStart(); Log.d(TAG, this.getApplicationContext().getPackageCodePath()); Intent service = new Intent(this.getApplicationContext(),LocalService.class); this.bindService(service, mSc, Context.BIND_AUTO_CREATE); } @Override protected void onStop() { super.onStop(); //must unbind the service otherwise the ServiceConnection will be leaked. <span style="color: rgb(255, 0, 0); ">this.unbindService(mSc);</span> } }
需要注意的是在onStop中要解绑定service, 否则会造成内存泄露的问题。
2. 我们再看一下与另外一个进程中的service进行通信的问题(跨进程通信!)。
如何将servcie运行在另外一个进程呢?在manifest 里面配置个属性就行了。
android:process=":remote" , 代表这个service运行在同一个应用程序的不同进程中。
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ckt.wangxin" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".LocalServiceTestActivity" 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=".LocalService"></service> <!-- android:process=":remote" specify this service run in another process in the same application. --> <service android:name=".RemoteService" android:process=":remote"></service> <activity android:name="RemoteServiceTestActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
public class RemoteServiceTestActivity extends Activity { static final String TAG = "RemoteServiceTestActivity"; ServiceConnection mSc; public static final int SAY_HELLO_TO_CLIENT = 0; /** * Handler of incoming messages from service. */ class IncomingHandler extends Handler { @Override public void handleMessage(Message msg) { switch (msg.what) { case SAY_HELLO_TO_CLIENT: Toast.makeText(RemoteServiceTestActivity.this.getApplicationContext(), "Hello World Remote Client!", Toast.LENGTH_SHORT).show(); break; default: super.handleMessage(msg); } } } Messenger messenger_reciever = new Messenger(new IncomingHandler()); /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mSc = new ServiceConnection(){ @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.d(TAG, "service connected"); <span style="color: rgb(204, 0, 0); ">Messenger messenger = new Messenger(service); Message msg = new Message(); msg.what = RemoteService.MSG_SAY_HELLO;</span> msg.replyTo = messenger_reciever; try { <span style="color: rgb(255, 0, 0); ">messenger.send(msg);</span> } catch (RemoteException e) { e.printStackTrace(); } } @Override public void onServiceDisconnected(ComponentName name) { Log.d(TAG, "service disconnected"); } }; } @Override protected void onStart() { super.onStart(); Log.d(TAG, this.getApplicationContext().getPackageCodePath()); Intent service = new Intent(this.getApplicationContext(),RemoteService.class); this.bindService(service, mSc, Context.BIND_AUTO_CREATE); } @Override protected void onStop() { super.onStop(); //must unbind the service otherwise the ServiceConnection will be leaked. this.unbindService(mSc); } }
获得service端传来的binder,用来构建一个Messenger向service发送消息。
public class RemoteService extends Service { public static final int MSG_SAY_HELLO = 0; @Override public IBinder onBind(Intent intent) { <span style="color: rgb(204, 0, 0); "> return messager.getBinder();</span> } Handler IncomingHandler = new Handler() { @Override public void handleMessage(Message msg) { if(msg.replyTo != null){ Message msg_client = this.obtainMessage(); msg.what = RemoteServiceTestActivity.SAY_HELLO_TO_CLIENT; try { ((Messenger)msg.replyTo).send(msg_client); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } switch (msg.what) { case MSG_SAY_HELLO: Toast.makeText(RemoteService.this.getApplicationContext(), "Hello World Remote Service!", Toast.LENGTH_SHORT).show(); break; default: super.handleMessage(msg); } } }; Messenger messager = new Messenger (IncomingHandler); }
构建一个Messenger,包含一个handler,然后将messenger的binder传给客户端,客户端可以通过handler再构造一个messenger与service通信,消息在handler里面被处理。
现在是service端单向响应客户端的消息,同理可以做成双向发送消息,实现双向通信。
发表评论
-
android-Service和Thread的区别
2013-08-23 09:17 876servie是系统的组件,它由系统进程托管(servicema ... -
git介绍
2013-08-01 14:49 972git介绍 使用Git的第一件事就是设置你的名字和email ... -
cocos2d-x学习之自动内存管理和常见宏
2013-07-29 15:41 8701.自动内存管理 1)概述 C++语言默认是 ... -
cocos2dx中利用xcode 调用java中的函数
2013-07-29 11:36 24831. 先把cocos2dx根目录中的 /Users/zhaos ... -
cocos2dx(v2.x)与(v1.x)的一些常用函数区别讲解
2013-07-29 10:35 1063第一个改动: CCLayer初始化 自定义Layer,类名 ... -
xcode与eclipse整合cocos2dx
2013-07-29 10:32 1185文档xcode版本是 204 1. 在xcode中创建coc ... -
git提交代码
2013-07-23 16:00 10031. 在本地创建一个Git的工作空间,在里面创建一个工程(如H ... -
Android.mk的用法和基础
2013-07-19 14:11 4285一个Android.mk file用来向编译系统描述你的源代码 ... -
eclipse配置NDK-Builder命令
2013-07-18 11:02 9821. 2. -
eclipse配置javah命令
2013-07-18 10:48 19561.找到javah命令所在的目录 我的为 /usr/bi ... -
Android SDL2.0 编译
2013-07-17 13:40 19231,下载: wget http://www.libsdl.o ... -
IntelliJ Idea 常用快捷键列表
2013-05-27 10:19 0Alt+回车 导入包,自动修 ... -
android应用后台安装
2013-05-21 12:02 924android应用后台安装,静默安装的代码实现方法 http ... -
编译linux内核映像
2013-05-21 11:33 921a)准备交叉编译工具链 android代码树中有一个pr ... -
如何单独编译Android源代码中的模块
2013-05-21 11:29 945一. 首先在Android源代码 ... -
Ubuntu安装JDK6和JDK5
2013-05-19 19:04 960sudo apt-get install sun-java6- ... -
java_jni详解_01
2013-05-08 17:15 909java中的jni 例子HelloWorld 准备过程: 1 ... -
下载android源码 中断解决原因
2013-05-07 15:51 1270解决方法 1. 浏览器登录https://android.go ... -
mac下编译ffmpeg1.1.4
2013-05-07 14:55 1321经过一番网上搜索 与 无数次的编译 终于成功了 下面献上编译 ... -
图片透明化处理
2013-01-12 11:27 1232MIDP2.0的图片编程中,可以把图片理解为一个int[]数组 ...
相关推荐
IPC方式之Binder连接池,DEMO自己根据任大神单独写的例子,可以更好的学习理解。
2.5 开发Android应用程序来使用硬件访问服务 第3章 智能指针 3.1 轻量级指针 3.1.1 实现原理分析 3.1.2 应用实例分析 3.2 强指针和弱指针 3.2.1 强指针的实现原理分析 3.2.2 弱指针的实现原理分析 ...
2.5 开发Android应用程序来使用硬件访问服务 第3章 智能指针 3.1 轻量级指针 3.1.1 实现原理分析 3.1.2 应用实例分析 3.2 强指针和弱指针 3.2.1 强指针的实现原理分析 3.2.2 弱指针的实现原理分析 3.2.3 ...
我们可以在AndroidManifest.xml文件中使用<service>标签来指定Service访问的权限: 1. <service class=”.service.MyService” android:permission=”com.wissen.permission.MY_SERVICE_PERMISSION”> 2. 3. ...
AndroidStudio实现(service)后台播放音乐(带有进度条) 思路:通过SevericeConnect来实现对audio.java 的mediaplay的调用再在Binder里建立player.的各类方法使得可以在Mainactivity里使用。 首先在res目录下新建...
2.5 开发Android应用程序来使用硬件访问服务.................................................. 44 第3章 智能指针............................................................... 49 3.1 轻量级指针..........
在Android系统中,一个进程是不能直接访问另一个进程的内存的,需要提供一些机制在不同的进程之间进行通信,Android官方推出了AIDL(Android Interface Definition Language),它是基于Binder机制的。 上篇提到组件在...
如何从C++代码中直接访问java的service示例代码
android计步器的实现,自定义的一个弧形进度条,记步通过手机的传感器来实现,也就是说不支持传感器的机子(应该很老的了吧)就没有效果。看看效果图: 这里写图片描述这里写图片描述 自定义View public class ...
当然,幕后还有binder、connection、cast local service,但你不必关心这些。 在视图(活动、按钮、视图或任何您认为是 mvc 模式中的“视图”的类)中,您还可以: 实现 HermesClient 及其方法,以访问您的控制器...