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 ...
随机推荐
- 【转】Open Live Writer 插件更新
一.更新内容 cnblog 页签中增加 Lua 和 Matlab 语法高亮:(SyntaxHighlight 页签中尚未添加) 增加折叠区域插件,见效果展示: 增加 bat 执行文件,直接拷贝插件所需 ...
- AIX 配置vncserver
我们安装数据库时,很多情况下客户现场并没有配置图形界面,这是就需要自己配置.vnc就是一个很好的工具vnc rpm包(vnc-3.3.3r2-6.aix5.1.ppc.rpm)下载地址为http:// ...
- WCF与Web API 区别(应用场景)
Web api 主要功能: 支持基于Http verb (GET, POST, PUT, DELETE)的CRUD (create, retrieve, update, delete)操作 请求的回 ...
- CSS3的几个标签速记1
border-radius:CSS3圆角 语法:border-radius:25px; 椭圆边角:语法-border-radius:xx%;或者15px/100px; box-shadow ...
- Container容器控件的使用、Hbox与Vbox布局管理器的使用、以及AjaxAction前后台事件响应
1.由于有前后台交互功能,需要在Spring上下文中注册一个用于提供服务的bean,对于这个bean使用Spring提供的@Component标注,如果需要使用@Component注解,需要在项目中W ...
- 严重: Exception starting filter struts2 java.lang.NullPointerException (转载)
严重: Exception starting filter struts2 java.lang.NullPointerException at com.opensymphony.xwork2.util ...
- VirtualBox single usermode boot
VirtualBox single usermode boot 当系统因为某些原因无法通过图形界面登录VirtualBox内的系统时,可以通过Grub进入命令行模式/单一用户界面模式. 参考: 1.R ...
- SVN查看提交日志的命令
Windows下,使用svn客户端查看日志很方便,但是如果在linux下,常规查看日志的命令对于日志记录数比较多的时候,屏幕显示不了,就比较麻烦,后来发现了一个很有用的参数可以限制要查看的记录条数 查 ...
- [翻译][MVC 5 + EF 6] 10:处理并发
原文:Handling Concurrency with the Entity Framework 6 in an ASP.NET MVC 5 Application 1.并发冲突: 当一个用户编辑一 ...
- Web前端新人笔记之jquery选择符
jquery利用了CSS选择符的能力,让我们能够在DOM中快捷而轻松的获取元素或元素集合.本章将介绍以下内容: 1.网页中的元素结构: 2.如何通过CSS选择符在页面中查找元素: 3.扩展jquery ...