Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >Android使用bindService作为中间人对象开启服务

Android使用bindService作为中间人对象开启服务

作者头像
Dream城堡
发布于 2019-01-28 02:52:21
发布于 2019-01-28 02:52:21
6190
举报
文章被收录于专栏:Spring相关Spring相关

Android使用bindService作为中间人对象开启服务

项目结构如下:

image

MyService:

<pre spellcheck="false" class="md-fences md-end-block contain-cm modeLoaded" lang="java" contenteditable="false" cid="n14" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: normal; display: block; break-inside: avoid; text-align: left; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">package com.demo.secondservice; ​ import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.widget.Toast; ​ public class MyService extends Service { public MyService() { } ​ @Override public IBinder onBind(Intent intent) { ​ ​ ​ ​ return new MyBind(); } ​ ​ public void banZheng(int money) { ​ if(money>1000){ Toast.makeText(getApplicationContext(), "帮你办", Toast.LENGTH_LONG).show(); ​ ​ }else { ​ Toast.makeText(getApplicationContext(), "钱少,不办", Toast.LENGTH_LONG).show(); ​ } } ​ //定义中间人 public class MyBind extends Binder{ ​ public void callBanZheng(int money){ ​ //调用办证的方法 banZheng(money); ​ } ​ ​ } } ​</pre>

MainActivity:

<pre spellcheck="false" class="md-fences md-end-block contain-cm modeLoaded" lang="java" contenteditable="false" cid="n25" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: normal; display: block; break-inside: avoid; text-align: left; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">package com.demo.secondservice; ​ import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.IBinder; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; ​ public class MainActivity extends AppCompatActivity { MyConn myConn; MyService.MyBind myBind; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent = new Intent(MainActivity.this, MyService.class); // startService(intent); //连接服务 myConn = new MyConn(); bindService(intent,myConn,BIND_AUTO_CREATE); } ​ public void click(View view) { ​ //自己new对象 脱离了谷歌框架 // MyService myService = new MyService(); myBind.callBanZheng(10200); ​ ​ } ​ ​ //监视服务的状态 private class MyConn implements ServiceConnection{ ​ ​ //当服务连接成功调用 @Override public void onServiceConnected(ComponentName name, IBinder service) { ​ myBind = (MyService.MyBind) service; ​ ​ } ​ //失去连接 @Override public void onServiceDisconnected(ComponentName name) { ​ } } ​ ​ @Override protected void onDestroy() { ​ //解绑服务 unbindService(myConn); super.onDestroy(); } } ​</pre>

activity_main.xml:

<pre spellcheck="false" class="md-fences md-end-block contain-cm modeLoaded" lang="java" contenteditable="false" cid="n36" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, "Liberation Mono", Courier, monospace; font-size: 0.9em; white-space: normal; display: block; break-inside: avoid; text-align: left; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(223, 226, 229); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> ​ <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> ​ <Button android:onClick="click" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开启服务" /> ​ </android.support.constraint.ConstraintLayout></pre>

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019.01.24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
Gatsby入门指南—添加博客文章列表(3)
1.查数据 <pre class="md-fences md-end-block" lang="" contenteditable="false" cid="c8" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, 'Liberation Mono', Courier, monospace; font-size: 0.9em; white-space: pre; display:
前端大彬哥
2019/05/29
4430
Gatsby入门指南—添加博客文章列表(3)
chalk.js(node终端样式库)
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="javascript" contenteditable="true" cid="n33" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">const chalk = reuquire("chalk") const log = console.log ​ // 颜色字体 log( chalk.red("红色") ) ​ // 背景色 log( chalk.bgBlue("蓝色背景") ) ​ // 样式字体 log( chalk.bold("加粗") ) ​ // 多参数 log( chalk.blue("name", "age", "job") ) ​ // ES6 多行文本 log( // 多行文本将保留缩进格式 chalk.blue(name: Rogan age: ${25} job: ${ 'IT' }) ) ​ ​ // 其他颜色设置方式 log(chalk.keyword("orange")(' keyword ')) // 关键字 log(chalk.rgb(100, 100, 100)(' rgb ')) // rgb log(chalk.hex('#ffffff')(' hex ')) // hex ​ ​ ​ // 样式组合 log(colors: ${chalk.blue('blue')}, ${chalk.red('red')}) // 拼接 ​ log(chalk.blue.bold("Title")) // 链式组合 ​ log( chalk.bgYellow(error: ${chalk.red(" chalk is undefined ")}) ) // 嵌套 ​ ​ ​ // 自定义组合 const error = chalk.bgRed; const warning = chalk.yellow.bold;</pre>
copy_left
2019/08/21
2.7K0
Java运行时数据区域
Java虚拟机在执行Java程序的过程中会把它所管理的内存划分为若干个不同的数据区域。其包括:程序计数器、Java虚拟机栈、本地方法栈、Java堆和方法区。
conanma
2021/12/06
3380
Python-requests模块学习笔记总结
前言一、requests模块使用1.1 requests模块发送get请求1.2 response响应对象1.3 response.text与response.content的区别1.4 通过对response.content进行decode,来解决中文乱码1.5 response响应对象的其他常用的属性和方法二、requests模块发送请求2.1 发送带headers的请求2.1.1思考2.1.2 携带请求头发送请求的方法2.2 发送带参数的请求2.2.1 在url携带参数2.2.2 通过params携带参数字典2.3 在headers参数中携带cookie2.3.1 github登录抓包分析3.3.2 完成代码2.4 cookie参数的使用2.5 cookiejar对象转换为cookies字典的方法2.6 超时timeout的使用2.7 代理proxies的使用2.7.1 理解使用代理的过程2.7.2正向代理和反向代理2.7.3 代理IP(代理服务器)的分类2.7.4 proxies代理参数的使用2.8 使用verify参数忽略CA证书三、 requests模块发送post请求3.1 requests发送post请求的方法四、利用requests.session进行状态保持4.1 requests.session的作用及应用场景4.2 requests.session的使用方法4.3 实例:模拟登录github精彩链接最后
conanma
2021/09/07
8650
golang http
<pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="go" contenteditable="true" cid="n98" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">router := http.NewServeMux() server := http.Server{}
copy_left
2019/08/21
3660
Gatsby入门指南—使用GraphQL解析Markdown(2)
GraphQL 既是一种用于 API 的查询语言也是一个满足你数据查询的运行时。 GraphQL 对你的 API 中的数据提供了一套易于理解的完整描述,使得客户端能够准确地获得它需要的数据,而且没有任何冗余,也让 API 更容易地随着时间推移而演进,还能用于构建强大的开发者工具。
前端大彬哥
2019/05/29
7710
Gatsby入门指南—使用GraphQL解析Markdown(2)
Gatsby入门指南—支持Markdown(1)
1.安装插件 我用Gatsby就是因为它支持Markdown.所以不墨迹,直接整Md支持。 <pre class="md-fences md-end-block" lang="js" contenteditable="false" cid="c10" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Consolas, 'Liberation Mono', Courier, monospace; fo
前端大彬哥
2019/05/25
5930
c++_std-numeric_limits极值接口
在C/C++11中,std::numeric_limits为模板类,在库编译平台提供基础算术类型的极值等属性信息。
上善若水.夏
2019/08/26
1.3K0
[C++]C风格、C++风格和C++11特性的线程池
thread pool就是线程的一种使用模式,一个线程池中维护着多个线程等待接收管理者分配的可并发执行的任务。
TOMOCAT
2022/04/01
5230
[C++]C风格、C++风格和C++11特性的线程池
在Centos7上安装Docker
在Centos7上安装Docker-ce直接用yum install docker -y安装的docker版本为1.12,但是docker发展很快,现在都18.03.1了。docker-ce是指docker的社区版。1、安装 yum-utils,它提供了 yum-config-manager,可用来管理yum源yum install -y yum-utils
Dream城堡
2018/09/10
1.3K0
Zookeeper 安装
ZooKeeper服务器是用Java创建的,它在JVM上运行。你需要使用JDK 6或更高版本。
用户5760343
2019/10/25
5440
bootstrap 中的 less
Bootstrap 的 CSS 文件是通过 Less 源码编译而来的。Less 是一门预处理语言,支持变量、mixin、函数等额外功能。
用户5760343
2019/07/05
7720
Linux logrotate 详细
志文件包含了关于系统中发生的事件的有用信息,在排障过程中或者系统性能分析时经常被用到。对于忙碌的服务器,日志文件大小会增长极快,服务器会很快消耗磁盘空间,这成了个问题。除此之外,处理一个单个的庞大日志文件也常常是件十分棘手的事。
用户5760343
2019/10/30
7590
Linux logrotate 详细
用10行Python代码,实现AI目标检测技术!(Python是最好的语言)
今天为大家带来的内容是,用10行Python代码,实现AI目标检测技术!(Python是最好的语言),本文具有不错的参考意义,希望能够帮助到你!
用户6133654
2019/10/10
7300
用10行Python代码,实现AI目标检测技术!(Python是最好的语言)
大佬的思路就是不一样,这是我见过最简洁又清晰的SSM框架整合
因为后面会配置springMVC,所以用IDEA的web骨架创建一个maven项目。
Java程序猿阿谷
2020/07/28
3890
大佬的思路就是不一样,这是我见过最简洁又清晰的SSM框架整合
实在是妙啊!Java中强软虚弱引用,居然还能这样去操作
前言 ThreadLocal 在什么情况下可能发生内存泄漏?如果你想清楚这个问题的来龙去脉,看源码是必不可少的,看了源码之后你发现, ThreadLocal 中用到 static class Entr
Java程序猿阿谷
2020/07/28
3890
实在是妙啊!Java中强软虚弱引用,居然还能这样去操作
markdown样式代码保存
/*此样式是没任何效果的,留给你填写的 你可以随意修改,组合你想要的css样式 没有最好的,只有最合适的, 看看后面的示例,你就会懂得写个你最爱的样式,并保存了的! 建议先复制某一你喜欢的css模板样式到此样式下,再在此基础上自定义自己的css样式。 来试试吧! ^_^*/
xing.org1^
2020/02/14
6610
Android框架-Google官方Gson解析
而 JSON (JavaScript Object Notation) 是一种轻量级的数据交换格式,易于人阅读和编写,同时也易于机器解析和生成,广泛应用于各种数据的交互中,尤其是服务器与客户端的交互。
Android技术干货分享
2019/03/26
1.1K0
Android框架-Google官方Gson解析
看阿里大牛深入浅出Java 线程池原理分析与使用
在我们的开发中“池”的概念并不罕见,有数据库连接池、线程池、对象池、常量池等等。下面我们主要针对线程池来一步一步揭开线程池的面纱。 使用线程池的好处 1、降低资源消耗 可以重复利用已创建的线程降低线程创建和销毁造成的消耗。 2、提高响应速度 当任务到达时,任务可以不需要等到线程创建就能立即执行。 3、提高线程的可管理性 线程是稀缺资源,如果无限制地创建,不仅会消耗系统资源,还会降低系统的稳定性,使用线程池可以进行统一分配、调优和监控 线程池的工作原理 首先我们看下当一个新的任务提交到线程池之后,线程池是如何
Java架构
2018/07/06
3440
python re
转载:https://www.runoob.com/python/python-reg-expressions.html
用户5760343
2019/11/11
8690
相关推荐
Gatsby入门指南—添加博客文章列表(3)
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文