以绑定的方式来启动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,捕捉到这个广播,然后可以进行相应的操作,比如:通过捕 ...
随机推荐
- SQL Server 4
一.视图 1.创建视图 1)选中数据库中的表中的视图处,右键选择新建视图,即: 2)在弹出“添加表”对话框中,单击“表”标签,选择要添加的表,点击添加,即: 3)选中要建立联系的列名的复选框,然后拖动 ...
- Taints和Tolerations联用,将pod部署到k8s的master节点
一般,k8s的master为了保持高性能,在这个主节点上只运行一些管理必须的POD. 如果我们限于资源,或是一些监控类的pod要部署到master节点呢? 昨天遇到这个问题,按网上通用的方法,未解决, ...
- 【转载】Java与C++语言在作用域上的差异浅析
http://developer.51cto.com/art/200906/126199.htm 差异一:变量作用域的不同 如下面这段程序代码是符合C++语言的语法要求的.其可以在C语言下正常运行.但 ...
- 004 @PathVariable映射URL绑定的占位符
一: 1.介绍 带占位符的URL是spring 3.0新增的功能,是向REST发展的重要阶段. @PathVariable可以将URL中占位符参数绑定到控制器处理的方法的入参中:URL中的{xxx}占 ...
- JSR教程2——Spring MVC数据校验与国际化
SpringMVC数据校验采用JSR-303校验. • Spring4.0拥有自己独立的数据校验框架,同时支持JSR303标准的校验框架. • Spring在进行数据绑定时,可同时调用校验框架完成数据 ...
- 002.SSH日常命令
一 远程登陆 ssh 用户名@远程主机ip:首次登陆需要下载对方公钥. 实例:ssh 192.168.10.129 二 远程复制 scp root@远程主机ip:[远程主机文件绝对路径] [需要保存的 ...
- C#并行编程(6):线程同步面面观
理解线程同步 线程的数据访问 在并行(多线程)环境中,不可避免地会存在多个线程同时访问某个数据的情况.多个线程对共享数据的访问有下面3种情形: 多个线程同时读取数据: 单个线程更新数据,此时其他线程读 ...
- Android activity之间数据传递和共享的方式之Application
1.基于消息的通信机制 Intent ---bundle ,extra 数据类型有限,比如遇到不可序列化的数据Bitmap,InputStream,或者LinkedList链表等等数据类型就不太好用 ...
- java基础学习总结——super关键字
一.super关键字
- Codeforces Round #370 (Div. 2) B. Memory and Trident 水题
B. Memory and Trident 题目连接: http://codeforces.com/contest/712/problem/B Description Memory is perfor ...