[Android学习笔记5]四大应用组件之一:Service 下
绑定方式的Service使用
在实现绑定服务时,最重要的是定义onBind()回调方法返回的接口,有三种方式:
1. 继承Binder类
2. 使用Messenger
3. 使用AIDL
这里对1,2方式进行分析。
1.继承Binder类
如果你的服务是你的应用程序的私有服务,并且跟客户端运行在同一个进程中,那么就应该通过继承Binder类来创建你的接口,并且佛从onBind()方法中返回这个接口的一个实例。客户端接收这个Binder对象,并且能够使用这个对象直接访问Binder类中实现的或Service中的公共方法。
使用官方SDK文档中的例子:
ServiceShow.java
package com.luoye.servicelearn; import java.util.Random; import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder; public class ServiceShow extends Service{ private final IBinder binder = new LocalBinder();//创建Service的内部IBinder对象 public class LocalBinder extends Binder
{
ServiceShow getService() //实现Binder自己的方法,这里返回Service对象的引用
{
return ServiceShow.this;
}
} public int getRandomNumber()
{
return (new Random()).nextInt(100);
} @Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
//返回一个IBinder对象,即Service的内部IBinder对象,作为ServiceConnection中onServiceConnected方法的IBinder传入
return binder;
} }
MainActivity.java
package com.luoye.servicelearn; import com.luoye.servicelearn.ServiceShow.LocalBinder; import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Menu;
import android.view.View;
import android.widget.Toast; public class MainActivity extends Activity { ServiceShow localService;
boolean bound = false; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} public void buttonOnClickListener(View v)
{
switch(v.getId())
{
case R.id.start_service1:
Intent intent_start = new Intent(this, ServiceShow.class);
//绑定Service 第二个参数为ServiceConnection对象,由于bindService是异步的,
//bindService()方法立即返回并且不返回IBinder对象,所以需要在ServiceConnection的方法中去处理
//BIND_AUTO_CREATE表示if Service不存在,即onCreate()
bindService(intent_start, connection, Context.BIND_AUTO_CREATE);
break;
case R.id.stop_service1:
if(bound)
{
unbindService(connection);
bound = false;
}//解绑Service
break;
case R.id.show_random:
if(bound == true)
{
Toast.makeText(this, "number is :"+localService.getRandomNumber(), Toast.LENGTH_SHORT).show();
}
break;
}
} private ServiceConnection connection = new ServiceConnection()
{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
LocalBinder binder = (LocalBinder)service; //得到service的IBinder对象
localService = binder.getService();//得到Service的引用,后续即可使用Service的方法
bound = true;
} @Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
bound = false;
} }; }
2. 使用Messenger
如果需要服务跟远程进程通信,那么就可以使用Messenger对象来给服务提供接口。
以下是信使(Messenger)对象的使用概要:
1. 服务端实现的一个处理器(Handler接口),这个处理器针对每次来自客户端的调用接收一次回调;
2. 这个处理器被用于创建一个信使对象(Messager)(这个信使对象要引用这个处理器);
3. 信使对象创建一个创建一个服务端从onBind()方法中返回给客户端的IBinder对象;
4. 客户端使用这个IBinder对象来实例化这个信使对象(信使引用了服务端的处理器),客户端使用这个信使给服务端发送Message对象;
5. 服务端在它的处理器(Handler)的handleMessage()方法中依次接收每个Message对象
在这种方法中,没有给客户端提供服务端的方法调用,相反,客户端会给发送服务端消息(Message)对象,服务端会在它的处理器中接受这些消息对象。
代码:
ServiceShow.java
package com.luoye.servicelearn; import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.widget.Toast; public class ServiceShow extends Service{ static final int MSG_CODE = 0x10;//message的消息类型定义 class IncomingHandler extends Handler //message处理器,应与信使绑定
{
public void handleMessage(Message msg)
{
switch(msg.what)
{
case MSG_CODE:
Toast.makeText(ServiceShow.this, "Hello world", Toast.LENGTH_SHORT).show();
break;
default:
super.handleMessage(msg);
}
}
}
//创建一个信使,该信使使用Handler对象作为msg的处理器
final Messenger messenger = new Messenger(new IncomingHandler()); @Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(ServiceShow.this, "Binding", Toast.LENGTH_SHORT).show();
return messenger.getBinder();//返回对应信使的Binder,返回给client使用
} }
MainActivity.java
package com.luoye.servicelearn; import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.view.Menu;
import android.view.View; public class MainActivity extends Activity { Messenger messenger;
boolean bound = false; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} public void buttonOnClickListener(View v)
{
switch(v.getId())
{
case R.id.start_service1:
Intent intent_start = new Intent(this, ServiceShow.class);
//绑定Service 第二个参数为ServiceConnection对象,由于bindService是异步的,
//bindService()方法立即返回并且不返回IBinder对象,所以需要在ServiceConnection的方法中去处理
//BIND_AUTO_CREATE表示if Service不存在,即onCreate()
bindService(intent_start, connection, Context.BIND_AUTO_CREATE);
if(bound)
{
//产生message对象
Message msg = Message.obtain(null, ServiceShow.MSG_CODE, 0, 0);
try
{
//使用client端的信使发送message,该消息会通过Binder对应的信使递交到Service端的
//信使,并由该端信使的消息处理器Handler进行处理
messenger.send(msg);
}
catch (Exception e)
{
e.printStackTrace();
}
}
break;
case R.id.stop_service1:
if(bound)
{
unbindService(connection);//解绑Service
bound = false;
}
break;
}
} private ServiceConnection connection = new ServiceConnection()
{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
messenger = new Messenger(service); //利用Service返回的信使的对应的IBinder来创建client端的信使对象
bound = true;
} @Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
messenger = null;
bound = false;
} }; }
[Android学习笔记5]四大应用组件之一:Service 下的更多相关文章
- [Android学习笔记4]四大应用组件之一:Service 上
一.什么是Service 一个Service就是一个能够在后台执行长时操作的应用程序组件,并且不提供用户界面.一个应用程序组件能够启动一个Service,即使用户切换到另一个应用程序,这个Servic ...
- Android学习笔记(十七)——数据库操作(下)
//此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! 这一次我们来试一试升级数据库,并进行数据库的CRUD操作,其中, C 代表添加(Create) ,R 代表查询 ...
- Android学习笔记(五一):服务Service(上)- IntentService
转自 http://blog.csdn.net/flowingflying/article/details/7616333 对于需要长期运行,例如播放音乐.长期和服务器的连接,即使已不是屏幕当前的ac ...
- Android学习笔记:使用ViewPager组件实现图片切换
在很多App中,尤其是第一次安装启动后,都会出现几个图片进行一些app的介绍和说明,图片可以随着滑动而切换. 我们这里利用 ViewPager组件来演示如何实现这一点. 1.创建一个app工程,默认创 ...
- Android学习笔记 ImageSwitcher图片切换组件的使用
activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&qu ...
- Android学习笔记 Toast屏幕提示组件的使用方法
activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&qu ...
- Android学习笔记(24):进度条组件ProgressBar及其子类
ProgressBar作为进度条组件使用,它还派生了SeekBar(拖动条)和RatingBar(星级评分条). ProgressBar支持的XML属性: Attribute Name Related ...
- Android学习笔记 TextSwitcher文本切换组件的使用
activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&qu ...
- 【转】 Pro Android学习笔记(七六):服务(1):local和remote
文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowingflying/ Android提供服务,服务是运行在后台的 ...
随机推荐
- 仿36氪(iOS版附源代码)
前言: 这是我2016年3月开始写的,利用课余时间全心投入的项目,本以为是凭着轻松愉悦的方式来学习的,中途遇到bug解决bug的时候,每天晚上几乎都是写到寝室关灯,还有一次使用Github不当写了五天 ...
- css伪类伪元素
在CSS中,模式(pattern)匹配规则决定哪种样式规则应用于文档树(document tree)的哪个元素.这些模式叫着选择符(selector). 一条CSS规则(rule)是选择符{属性:值; ...
- java中对象模型与数据库中的关系模型
实体类还需要配置到hibernate.cfg.xml中,以便Hibernate初始化实体类与数据库表的映射关系.如果只配置了映射关系,而没有配置到hibernate.cfg.xml中,Hibernat ...
- java Serialization and Deserializaton
This article from JavaTuturial Java provides a mechanism, called object serialization where an objec ...
- python10min系列之面试题解析:python实现tail -f功能
同步发布在github上,跪求star 这篇文章最初是因为reboot的群里,有人去面试,笔试题有这个题,不知道怎么做,什么思路,就发群里大家讨论 我想了一下,简单说一下我的想法吧,当然,也有很好用的 ...
- Qt之界面美化输入框(使用QSS确实非同凡响)
我们先看下酷狗音乐的输入框效果图: 这里我们需要实现在输入框里面添加一个按钮(多个也一样),并且提供一个默认时候的文字..直接上代码好了. QHBoxLayout *lay = new QHBoxLa ...
- hive 0.10 0.11新增特性综述
我们的hive版本升迁经历了0.7.1 -> 0.8.1 -> 0.9.0,并且线上shark所依赖的hive版本也停留在0.9.0上,在这些版本上有我们自己的bug fix patch和 ...
- opencv中遇到的的一些错误
一:错误提示:OpenCV Error:Bad argument<src and dst have different formats> in unkown function,file.. ...
- debian安装vld来查看Opcode,PHP调优。
一: 我的环境: Debian 7 (wheezy) x64 PHP 5.4.4-14 (apt-get 而来) Apache/2.2.22 (同上,非源码编译) 二 :安装vld. (# 代表是r ...
- HDU 3746 Cyclic Nacklace KMP
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3746 KMP算法—— AC代码: #include <iostream> #include ...