以绑定的方式来启动service
先说下原理,之前我们的启动service就是用startService来启动的,这是显式启动。启动后我们无法得到service中的数据,也无法知道它执行的状态,如果我们要启动它的activity和它建立一个联系,获得他的数据或者是执行其内部的方法时就需要隐式启动了。
关键原理在于使用一个binder来传递数据,并且要给service配置一个action作为标签。
BindService
package com.example.service; import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log; public class BindService extends Service{ String tag = getClass().getSimpleName(); private int count = 123;
private MyBinder binder = new MyBinder(); //建立一个binder对象,用来在onbind中返回
public class MyBinder extends Binder{
public int getCount() {
return count;
}
} /* (非 Javadoc)
* @see android.app.Service#onBind(android.content.Intent)
* 连接时执行的方法
*/
@Override
public IBinder onBind(Intent intent) {
// TODO 自动生成的方法存根
Log.i(tag, "onBind");
return binder;
} /* (非 Javadoc)
* @see android.app.Service#onUnbind(android.content.Intent)
* 断开连接时回调方法
*/
@Override
public boolean onUnbind(Intent intent) {
// TODO 自动生成的方法存根
Log.i(tag, "onUnbind");
return super.onUnbind(intent);
} @Override
public void onCreate() {
// TODO 自动生成的方法存根
super.onCreate();
Log.i(tag, "onCreat");
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO 自动生成的方法存根
Log.i(tag, "onStartCommand");
return super.onStartCommand(intent, flags, startId);
} @Override
public void onDestroy() {
// TODO 自动生成的方法存根
super.onDestroy();
Log.i(tag, "onDestroy");
} }
这里binder就可以通过其内部的getcount方法来向外部传递数据了。接下来要配置service了,这里的action就是用来给启动时作为标记的。
<service android:name="com.example.service.BindService" >
<intent-filter>
<action android:name="com.example.service.BindService" />
</intent-filter>
</service>
最后是在activity中通过button来启动service

package com.example.service; import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View; public class MainActivity extends Activity { private BindService.MyBinder binder;
private ServiceConnection connection = new ServiceConnection() { @Override
public void onServiceConnected(ComponentName name, IBinder service) {
//连接成功时,获取service的binder对象
binder = (BindService.MyBinder)service;
} @Override
public void onServiceDisconnected(ComponentName name) {
// TODO 断开连接时
}
}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); } public void buttonListener(View v) {
//设置action,这样启动的时候就知道是启动哪个service了
Intent intent = new Intent("com.example.service.BindService");
switch (v.getId()) {
case R.id.binderService_button:
//通过这个过滤器来判断绑定哪个service,所以在配置的时候需要配置action
bindService(intent, connection, Service.BIND_AUTO_CREATE);
break;
case R.id.unbinderService_button:
unbindService(connection);
break;
case R.id.getData_button:
System.out.println("service 中的数据 = :"+ binder.getCount());
break;
default:
break;
}
}
}
以绑定的方式来启动service的更多相关文章
- android 在5.0以后不允许使用隐式Intent方式来启动Service
android5.0以后不能使用隐式intent :需要指定Intent的ComponentName信息:intent.setComponent(xxx),或指定Intent的setPackage(& ...
- Android为TV端助力 android 在5.0以后不允许使用隐式Intent方式来启动Service
android5.0以后不能使用隐式intent :需要指定Intent的ComponentName信息:intent.setComponent(xxx),或指定Intent的setPackage(& ...
- 如何启动Service,如何停用Service(转)
如何启用Service,如何停用Service Android中的服务和windows中的服务是类似的东西,服务一般没有用户操作界面,它运行于系统中不容易被用户发现,可以使用它开发如监控之类的程序.服 ...
- Android动态部署五:怎样从插件apk中启动Service
转载请注明出处:http://blog.csdn.net/ximsfei/article/details/51072332 github地址:https://github.com/ximsfei/Dy ...
- asp.net core容器&mysql容器network互联 & docker compose方式编排启动多个容器
文章简介 asp.net core webapi容器与Mysql容器互联(network方式) docker compose方式编排启动多个容器 asp.net core webapi容器与Mysql ...
- Android中Service通信(一)——启动Service并传递数据
启动Service并传递数据的小实例(通过外界与服务进行通信): 1.activity_main.xml: <EditText android:layout_width="match_ ...
- 重新想象 Windows 8 Store Apps (54) - 绑定: 增量方式加载数据
[源码下载] 重新想象 Windows 8 Store Apps (54) - 绑定: 增量方式加载数据 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 绑定 通过实 ...
- Binding 中 Elementname,Source,RelativeSource 三种绑定的方式
在WPF应用的开发过程中Binding是一个非常重要的部分. 在实际开发过程中Binding的不同种写法达到的效果相同但事实是存在很大区别的. 这里将实际中碰到过的问题做下汇总记录和理解. 1. so ...
- 安卓开机启动service后台运行
安卓开机启动service后台运行 Android开机启动时会发送一个广播android.intent.action.BOOT_COMPLETED,捕捉到这个广播,然后可以进行相应的操作,比如:通过捕 ...
随机推荐
- CSS------给字体添加边框时,边框大小无法改变问题
如图: 代码:(需要将display属性设置为inline-block,在设置height和line-height调整位置) //品牌点击 $(".li-brand").click ...
- HTML5本地存储localStorage、sessionStorage及IE专属UserData
By:王美建 from:http://www.cnblogs.com/wangmeijian/p/4518606.html 转载请保留署名和出处! 在客户端存储数据用的最普遍的方式非cookie莫属, ...
- MySQL DROP DB或TABLE场景下借助SQL Thread快速应用binlog恢复方案
[问题] 假设有这种场景,误操作DROP DB或TABLE,常规的恢复操作是还原全备份,并用mysqlbinlog追加到drop操作前的位置. 如果需要恢复的binlog的日志量比较大而我们只希望恢复 ...
- 入门智能家居,从 IFTTT 到 HomeKit 自动化(二)
入门智能家居,从 IFTTT 到 HomeKit 自动化(二) 目录 0. HomeKit.HomeBridge.HomeAssistant 分别是什么?关系是什么? 1. 开始前的准备 2. 整 ...
- zookeeper 节点启动时的更新机制
使用zk的应用节点和zk数据本身的同步,当系统启动时使用zk配置的信息和zk本身存储不一致, 此时应存在一个更新机制将应用配置数据和zk数据更新一致. 启动时更新拉取zk配置中心的更新本地数据,以zk ...
- "Unchecked-Send"漏洞分析
author:sf197tl;dr国内并没有一个文档有讲述该漏洞的,正好闲着没事.就写下这篇文章.在网上也搜寻了一些资料,通过自己的翻译才有今天的这篇文章.该漏洞在DASP TOP 10中可以查看到. ...
- C#快速找出磁盘内的所有文件
本文只针对NTFS格式化的磁盘文件快速检索,速度不是非常快,是让你震惊. 一般用文件遍历的方法检索一个50G的文件夹需要几十分钟甚至一个小时的时间,而用本方法只需几秒. using System; u ...
- Go 面试题(附答案解析)
1.写出下面代码输出内容 package main import ( "fmt" ) func main() { defer_call() } func defer_call() ...
- 依赖注入(DI)和控制反转(IOC)的理解,写的太好了。
学习过spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的,今天和大家 ...
- [Winform]setupfactory打包时添加开机自启动的脚本
摘要 如果有这样的需求,需要软件开机自启动,该如何做呢?开机自启动的做法,就是修改注册表,将你的exe注册到注册表Run节点下. setupfactory 在安装的时候需要以管理员身份运行,这样可以保 ...