Android BinderService 过程
步骤:建立服务器端服务,暴露接口
1.BinderService
/**
* @Title BinderService.java
* @package cn.boxai.binderservice
* @since
* @version 1.0.0
* @author Vic Lee
* @date Aug 14, 2016-3:50:04 PM
*/
package cn.boxai.binderservice; import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder; public class BinderService extends Service { //step:1 difind mBinder
IBinder mBinder=new MyBinder(); //step:3 implements mBinder //step:2 difine MyBinder and extends
class MyBinder extends Binder
{
//调用其他的服务
public BinderService getService()
{
return BinderService.this;
}
public String helloWorld(String name)
{
return "You name is :"+name;
}
}
31
//添加其他的服务
public void helloservice()
{
for (int i = 0; i < 100; i++)
{
try {
Thread.sleep(1000);
Log.i("helloservice", i+"");
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
//step:4 retrue mBinder Exposed interface
@Override
public IBinder onBind(Intent intent) { return mBinder; } }
2.客户端
package cn.boxai.binderservice; import cn.boxai.binderservice.BinderService.MyBinder;
import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast; public class BinderServiceActivity extends Activity {
Button mBinderServiceButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBinderServiceButton=(Button) findViewById(R.id.btn_BinderService);
mBinderServiceButton.setOnClickListener(new OnClickListener()
{ @Override
public void onClick(View v)
{
// step 5:call interface from server
//bindService parameter=>_intent
Intent _intent=new Intent(BinderServiceActivity.this,BinderService.class);
bindService(_intent, conn, BinderService.BIND_AUTO_CREATE);
}
}); }
//step 6:bindService parameter=>conn
private ServiceConnection conn=new ServiceConnection()
{ @Override
public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub } @Override
public void onServiceConnected(ComponentName name, IBinder service) { //step 7:receive server method
String bindername=((MyBinder)service).helloWorld("LD");
Toast.makeText(BinderServiceActivity.this, bindername, 3000).show();
Log.i("BinderService", "BinderService==>"+bindername); ((MyBinder)service).getService().helloservice();//绑定其他服务
}
}; }
取消绑定
客户端
Button mUnBinderServiceButton; //取消绑定
mUnBinderServiceButton=(Button) findViewById(R.id.btn_UnBinderService);
mUnBinderServiceButton.setOnClickListener(new OnClickListener()
{ @Override
public void onClick(View v)
{
unbindService(conn); }
});
服务端端
@Override
public boolean onUnbind(Intent intent) { // TODO Auto-generated method stub
return super.onUnbind(intent); }
Android BinderService 过程的更多相关文章
- Cocos2dx-3.0版本 从开发环境搭建(Win32)到项目移植Android平台过程详解
作为重量级的跨平台开发的游戏引擎,Cocos2d-x在现今的手游开发领域占有重要地位.那么问题来了,作为Cocos2dx的学习者,它的可移植特性我们就需要掌握,要不然总觉得少一门技能.然而这个时候各种 ...
- Android系统启动过程-uBoot+Kernel+Android
摘要:本文是参考大量网上资源在结合自己查看源代码总结出来的,让自己同时也让大家加深对Android系统启动过程有一个更加深入的了解!再次强调,本文的大多数功劳应归功于那些原创者们,同时一些必要的参考链 ...
- Android 核心分析 之八Android 启动过程详解
Android 启动过程详解 Android从Linux系统启动有4个步骤: (1) init进程启动 (2) Native服务启动 (3) System Server,Android服务启动 (4) ...
- Android启动过程以及各个镜像的关系
Android启动过程 Android在启动的时候,会由UBOOT传入一个init参数,这个init参数指定了开机的时候第一个运行的程序,默认就是init程序,这个程序在ramdisk.img中.可以 ...
- Android编译过程详解(一)
Android编译过程详解(一) 注:本文转载自Android编译过程详解(一):http://www.cnblogs.com/mr-raptor/archive/2012/06/07/2540359 ...
- Android(java)学习笔记162:Android启动过程(转载)
转载路径为: http://blog.jobbole.com/67931/ 1. 关于Android启动过程的问题: 当按下Android设备电源键时究竟发生了什么? Android的启动过程是怎么样 ...
- Android 安装过程中的问题
Android 安装过程中的问题 上一篇我说到配置android环境,但是在具体的安装过程中,因为下载的软件或者方法不同,导致没有正确的结果,如果有一些错误的时候,可以试一试关闭eclipse软件, ...
- Android 系统启动过程简单记录
本文记录Android系统启动过程,包含从linux kernerl到luancher启动完成的过程: 1.linux内核完成系统设置后,会在系统文件中寻找‘init’文件,然后启动root进程或者说 ...
- Android 深入浅出 - Android系统启动过程
Activity的类继承关系及跟踪Activity的启动 Android系统启动过程 https://study.163.com/course/courseLearn.htm?courseId=213 ...
随机推荐
- margin重叠现象与margin auto自适应居中
上下相邻的(算一种兄弟关系)普通元素,上下边距并非简单的相加,而是取其中最大的边距值:而浮动的盒子边距是相加的:父子div也会发生重叠,并不是bug: <style>#test1{ wid ...
- tomcat由浅入深
零.服务器.Servlet容器.web容器 Servlet容器:能够运行Servlet的环境叫做Servlet容器 web容器:能够运行web应用的环境就叫做web容器 weblogic websph ...
- MyEclipse 及Tomcate 安装 配置
使用的工具为myeclipse-pro-2014-GA-offline-installer-windows(需安装).apache-tomcat-6.0.37.jdk1.6.0.14. 1.MyEcl ...
- Spring操作指南-IoC基础环境配置(基于注解自动装配)
项目源码:http://code.taobao.org/p/LearningJavaEE/src/LearningSpring001%20-%20Automatically%20wiring%20be ...
- New line
The concepts of line feed (LF) and carriage return (CR) are closely associated, and can be either co ...
- storage disk
scsi fdisk -l can not display the new disk Rescan the SCSI Bus to Add a SCSI Device Without rebootin ...
- 【皇甫】☀ 图_substring
substring是啥?
- 论文阅读(Xiang Bai——【PAMI2017】An End-to-End Trainable Neural Network for Image-based Sequence Recognition and Its Application to Scene Text Recognition)
白翔的CRNN论文阅读 1. 论文题目 Xiang Bai--[PAMI2017]An End-to-End Trainable Neural Network for Image-based Seq ...
- python学习笔记系列----(五)输入和输出
这一章主要是讲述程序展示其数据的一些方法,一般都是直接按照一定的格式输出在屏幕,或者写入到文件以便以后使用.按照一定格式的输出,在python中实际就是对str的操作,主要就是介绍了formart() ...
- Remove-Azureaccount (Can't get Azure credentials to stick in Powershel)
https://social.technet.microsoft.com/forums/azure/en-US/260df055-7c4e-4ce2-8f8d-190ad20a4b76/cant-ge ...