以绑定的方式来启动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,捕捉到这个广播,然后可以进行相应的操作,比如:通过捕 ...
随机推荐
- T-SQL语句1
一.创建表 1.创建常见表 create table tablename ( Column_name1 dataType, Column_name1 dataType, Column_name1 da ...
- mysql多表查询,group by并将结果导出来csv文件
SQL手动操作得少,遇到一个CASE,就记录一个CASE吧. 今天遇到的是统计一个发布结果. 这个发布表中,有两个字段是外键,并且要求按其中一个外键,作group by操作, 最后,导出来excel能 ...
- 007.MySQL-Keepalived搭配脚本01
vim /etc/keepalived/check_MySQL.sh #!/bin/bash MYSQL=/usr/bin/mysql MYSQL_HOST=localhost MYSQL_USER= ...
- 谈 JavaScript 中的强制类型转换 (1. 基础篇)
1. 从基本包装类型讲起 讨论基本包装类型的前提是了解基本数据类型(也可以称为原始类型, 简单数据类型等).然后通过基本数据类型调用方法的行为, 引出基本包装类型的概念和作用. 1.1 基本数据类型 ...
- Javascript版经典游戏之《扫雷》
翻出年初写的游戏贴上来,扫雷相信大家都玩过,先上图: 源码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN ...
- JSONObject 自定义过滤配置
一.自定义过滤器说明 PropertyPreFilter 根据PropertyName判断是否序列化 PropertyFilter 根据PropertyName和PropertyValue来判断是否 ...
- 【BZOJ-3532】Lis 最小割 + 退流
3532: [Sdoi2014]Lis Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 704 Solved: 264[Submit][Status] ...
- 【bzoj 1076】【SCOI2008】奖励关
1076: [SCOI2008]奖励关 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1602 Solved: 891[Submit][Status ...
- CentOS下的apache配置支持php
修改Apache的配置文件httpd.conf(vi /etc/httpd/conf/httpd.conf) DirectoryIndex index.html index.php #添加index. ...
- Windows访问Linux的Ext4格式分区
Ext2Fsd是Windows下一套很实用的Driver,虽然名称是ext2fsd但ext3/ext4都可读取,安装完成后电脑便可直接认得ext格式扇区 虽然官方介绍只能支持到Windows 8,但实 ...