ANDROID_MARS学习笔记_S01原始版_017_绑定SERVICE
一、流程
1.编写service,重写onBind(Intent intent),返回自定义的Binder
2.自写义Binder,提供一个可访问的方法,以传递数据
3.点击界面按钮会开启service,过程为,通过Intent的setClass指定要开启的service,再通过bindService(intent, conn, BIND_AUTO_CREATE);开启service,其中参数conn是ServiceConnection,需要实现它的onServiceConnected(ComponentName name, IBinder service),service开启成功后,会回调这个函数,会把binder传给参数IBinder,从而实现数据传递,这就是所谓的给service提供了client-server模式
二、代码
1.xml
(1)activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.service2.MainActivity" > <Button
android:id="@+id/bindBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="完成绑定操作" /> </RelativeLayout>
(2)AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.service2"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
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=".SecondService"></service>
</application> </manifest>
2.java
(1)MainActivity.java
package com.service2; 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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; import com.service2.SecondService.FirstBinder; public class MainActivity extends Activity { private Button bindBtn = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindBtn = (Button)findViewById(R.id.bindBtn);
bindBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, SecondService.class);
bindService(intent, conn, BIND_AUTO_CREATE);
}
});
} ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
System.out.println("onServiceDisconnected>>>>>>");
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
FirstBinder binder = (FirstBinder)service;
System.out.println("onServiceConnected>>>>>"+binder.getData());
}
};
}
(2)SecondService.java
package com.service2; import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder; public class SecondService extends Service { @Override
public IBinder onBind(Intent intent) {
IBinder binder = new FirstBinder();
return binder;
} class FirstBinder extends Binder {
public String getData() {
return "test data";
}
}
}
ANDROID_MARS学习笔记_S01原始版_017_绑定SERVICE的更多相关文章
- ANDROID_MARS学习笔记_S01原始版_019_SERVICE之Transact
一.代码1.xml(1)activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/ ...
- ANDROID_MARS学习笔记_S01原始版_013_广播机制二
一.代码1.xml(1)main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayo ...
- ANDROID_MARS学习笔记_S01原始版_008_Looper\Bundle异步消息处理
一.流程 1.自定义Handler,重写handleMessage(Message msg),用msg得到bundle,从而得到传递过来的数据 2.开启android.os.HandlerThread ...
- ANDROID_MARS学习笔记_S01原始版_005_RadioGroup\CheckBox\Toast
一.代码 1.xml(1)radio.xml <?xml version="1.0" encoding="utf-8"?> <LinearLa ...
- ANDROID_MARS学习笔记_S01原始版_004_TableLayout
1.xml <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android ...
- ANDROID_MARS学习笔记_S01原始版_003_对话框
1.AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest ...
- ANDROID_MARS学习笔记_S01原始版_002_实现计算乘积及menu应用
一.代码 1.xml(1)activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk ...
- ANDROID_MARS学习笔记_S01原始版_001_Intent
一.Intent简介 二.代码 1.activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.co ...
- ANDROID_MARS学习笔记_S01原始版_023_MP3PLAYER005_用广播BroacastReciever实现后台播放不更新歌词
一.代码流程1.自定义一个AppConstant.LRC_MESSAGE_ACTION字符串表示广播"更新歌词" 2.在PlayerActivity的onResume()注册Bro ...
随机推荐
- 用JQuery中的Ajax方法获取web service等后台程序中的方法
用JQuery中的Ajax方法获取web service等后台程序中的方法 1.准备需要被前台html页面调用的web Service,这里我们就用ws来代替了,代码如下: using System; ...
- SQL IF UPDATE(Number) 在触发器[Trigger]中的用法
IF UPDATE(Number)在触发器(TRIGGER)中用处很大 CREATE TRIGGER TrgTest ON [TableName] FOR UPDATE IF UPDATE(Colum ...
- FusionChart 导出图片 功能实现(转载)
FusionChart 导出图片 功能实现(转载) http://www.cnblogs.com/jiagoushi/archive/2013/02/05/2893468.html 题目:精美Fusi ...
- ###学习《Effective C++》
开源中国. #@date: 2014-06-16 #@author: gerui #@email: forgerui@gmail.com 前几天买了好几本书,其中有一本是<Effective C ...
- iOS获取webview高度
int webHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.scr ...
- EasyUI的DataGrid 分页栏英文改中文解决方案
(一)分页栏英文改中文解决方案 这个问题其实很简单,就是引入文件jquery-easyui-1.3/locale/easyui-lang-zh_CN.js . 注意这个文件要放在本页js的后面,放在最 ...
- Hdu 1042 N! (高精度数)
Problem Description Givenan integer N(0 ≤ N ≤ 10000), your task is to calculate N! Input OneN in one ...
- 【HeadFirst设计模式】11.代理模式
定义: 为另一个对象提供一个替身或占位符以访问这个对象. 远程代理 虚拟代理 保护代理.动态代理 其它: 远程代理的远程接口必须继承记号接口Remote 远程代理的使用过程中,要注意序列化的问题, 远 ...
- Linux C 程序 函数,数组,指针,gdb调试器(SEVEN)
函数,数组,指针,gdb调试器 1.函数定义 如果明确指定返回类型,默认为int 参数传递:实参对形参的参数传递是单向的,实参只是把自己的值赋给形参. 形参的 ...
- A-frame_02
A-Frame 让我们能够仅仅通过几行HTML代码创建出可以运行在桌面, 虚拟眼镜, 以及手机上的VR场景. 而且因为这个框架是基于HTML的, 我们也可以像一般的HTML元素一样配合JavaScri ...