先说下原理,之前我们的启动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的更多相关文章

  1. android 在5.0以后不允许使用隐式Intent方式来启动Service

    android5.0以后不能使用隐式intent :需要指定Intent的ComponentName信息:intent.setComponent(xxx),或指定Intent的setPackage(& ...

  2. Android为TV端助力 android 在5.0以后不允许使用隐式Intent方式来启动Service

    android5.0以后不能使用隐式intent :需要指定Intent的ComponentName信息:intent.setComponent(xxx),或指定Intent的setPackage(& ...

  3. 如何启动Service,如何停用Service(转)

    如何启用Service,如何停用Service Android中的服务和windows中的服务是类似的东西,服务一般没有用户操作界面,它运行于系统中不容易被用户发现,可以使用它开发如监控之类的程序.服 ...

  4. Android动态部署五:怎样从插件apk中启动Service

    转载请注明出处:http://blog.csdn.net/ximsfei/article/details/51072332 github地址:https://github.com/ximsfei/Dy ...

  5. asp.net core容器&mysql容器network互联 & docker compose方式编排启动多个容器

    文章简介 asp.net core webapi容器与Mysql容器互联(network方式) docker compose方式编排启动多个容器 asp.net core webapi容器与Mysql ...

  6. Android中Service通信(一)——启动Service并传递数据

    启动Service并传递数据的小实例(通过外界与服务进行通信): 1.activity_main.xml: <EditText android:layout_width="match_ ...

  7. 重新想象 Windows 8 Store Apps (54) - 绑定: 增量方式加载数据

    [源码下载] 重新想象 Windows 8 Store Apps (54) - 绑定: 增量方式加载数据 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 绑定 通过实 ...

  8. Binding 中 Elementname,Source,RelativeSource 三种绑定的方式

    在WPF应用的开发过程中Binding是一个非常重要的部分. 在实际开发过程中Binding的不同种写法达到的效果相同但事实是存在很大区别的. 这里将实际中碰到过的问题做下汇总记录和理解. 1. so ...

  9. 安卓开机启动service后台运行

    安卓开机启动service后台运行 Android开机启动时会发送一个广播android.intent.action.BOOT_COMPLETED,捕捉到这个广播,然后可以进行相应的操作,比如:通过捕 ...

随机推荐

  1. linux环境vnc安装

    环境:centos6.9 背景:有时安装软件图形化方便操作 1.安装vnc服务端 yum install tigervnc-server -y 2.修改vncserver的配置文件.命令:vim /e ...

  2. 【BZOJ】4561: [JLoi2016]圆的异或并

    题解 我们把圆拆成两个圆弧,按照圆弧的左右端点排序来增加和删除 那么我们把圆弧按照纵坐标排序,一定是两两不相交的 我们新加入一个圆的时候,找上圆弧的前驱,如果前驱是一个上圆弧,那么这个上圆弧所在的圆就 ...

  3. 本机Tomcat启动myeclipse,用Jmeter录制脚本端口冲突解决办法

    今天用jmeter 录制脚本与已经启动的Tomcat端口冲突,无法启动工作台的http代理服务器, 如果两个的端口一样,则http代理服务器启动就会提示端口被占用 所以先把Tomcat端口和http代 ...

  4. 查询物理表字段(mysql)

    SELECT t.COLUMN_NAME AS NAME, ( CASE WHEN t.IS_NULLABLE = 'YES' THEN '1' ELSE '0' END ) AS ISNULL, ( ...

  5. Git的一些常用命令

    一:Git是什么? Git是目前世界上最先进的分布式版本控制系统. 简单的说就是托管代码的便于多人开发的管理系统. 二.Git的一些命令,我详细的说一下 我是基于github给大家说一下git的一些常 ...

  6. poj 2253 最短路floyd **

    题意:有两只青蛙和若干块石头,现在已知这些东西的坐标,两只青蛙A坐标和青蛙B坐标是第一个和第二个坐标,现在A青蛙想要到B青蛙那里去,并且A青蛙可以借助任意石头的跳跃,而从A到B有若干通路,问从A到B的 ...

  7. oracle切割以,隔开的数字字符串

    提前声明strsplit_typeCREATE OR REPLACE TYPE strsplit_type as table of varchar2(4000); 如果不,会报错:PLS-00201: ...

  8. centos 7.2 安装mysql 修改 初始密码

    # /etc/init.d/mysqld stop # mysqld_safe --user=mysql --skip-grant-tables --skip-networking &# my ...

  9. 使用 IntraWeb (15) - 基本控件之 TIWEdit、TIWMemo、TIWText

    TIWEdit //单行文本框, 通过 PasswordPrompt 属性可以作为密码框 TIWMemo //多行文本框 TIWText //相当于多行的 TIWLabel 或不能编辑的 TIWMem ...

  10. Android四种Activity的加载模式

    建议首先阅读下面两篇文章,这样才可以更好的理解Activity的加载模式: Android的进程,线程模型 http://www.cnblogs.com/ghj1976/archive/2011/04 ...