当前位置: 首页 > news >正文

android Service 与 activity 通信 并不断传数据

注:这只是个Demo 

 以下载为案例,实现开启下载,暂停下载,下载进度不断发送给activity

class DownloadService : Service() {override fun onBind(intent: Intent?): IBinder? {return MyBinder()}inner class MyBinder : Binder(){val service:DownloadServiceget() = this@DownloadService}private var mCurrentProgress:Int = 0var isStart = AtomicBoolean() fun startDownload(progress:(progress:Int)-> Unit){if (!isStart.get()){Log.d("downloadService","进度:开启")isStart.set(true)Thread(object :Runnable{override fun run() {while (isStart.get()) {try { if (mCurrentProgress<100){mCurrentProgress ++}if (mCurrentProgress>=100){isStart.set(false)}progress.invoke(mCurrentProgress) Thread.sleep(1000)} catch (e: InterruptedException) {throw RuntimeException(e)}}}} ).start() } }fun stopDownload(){if (isStart.get()){Log.d("downloadService","进度:暂停")isStart.set(false)}}}

在清单文件配置<manifest xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"><application ><service android:name=".activity.DownloadService"/></application></manifest>


class DownloadActivity : Activity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_download)findViewById<View>(R.id.btnStart).setOnClickListener {startDownload()}findViewById<View>(R.id.btnStop).setOnClickListener {stopDownload()}}var isStartService = AtomicBoolean()fun startDownload(){if (!isStartService.get()){isStartService.set(true)Intent(this@DownloadActivity,DownloadService::class.java).apply {this.putExtra("data1","数据1")}.let {bindService(it,serviceConnection,Context.BIND_AUTO_CREATE)}}else{downloadService?.startDownload {progress->Log.d("downloadService","++ 进度:${progress}%")}}}fun stopDownload(){downloadService?.stopDownload()}var downloadService:DownloadService? = nullvar serviceConnection = object :ServiceConnection{override fun onServiceConnected(p0: ComponentName?, iBinder: IBinder?) {downloadService =  (iBinder as DownloadService.MyBinder).servicedownloadService?.startDownload {progress->Log.d("downloadService","进度:${progress}%")}}override fun onServiceDisconnected(p0: ComponentName?) {}}override fun onDestroy() {super.onDestroy()stopDownload()}
}

结果

http://www.lryc.cn/news/308599.html

相关文章:

  • Acwing-基础算法课笔记之数学知识(扩展欧几里得算法)
  • 简单排列组合题(python版)
  • 【排坑】搭建 Karmada 环境
  • 每日一类:Qt GUI开发的基石《QWidget》
  • 人大金仓与mysql的差异与替换
  • Excel2LaTeX插件的使用、LaTeX表格
  • MySQL 用了哪种默认隔离级别,实现原理是什么?
  • 【C++初阶】第四站:类和对象(下)(理解+详解)
  • redis的基本数据类型(一)
  • Windows无法识别exFAT格式怎么办?
  • AI大模型的发展趋势?
  • List去除重复数据的五种方式
  • VUE3自定义文章排行榜的简单界面
  • 七通道NPN 达林顿管GC2003,专为符合标准 TTL 而制造,最高工作电压 50V,耐压 80V
  • 若依springboot接入feign踩坑记录
  • Lumerical Script ------ Error: <文件目录> line x:syntax error
  • Opencv基础与学习路线
  • Python装饰器的使用详解
  • 基于springboot+vue的党员教育和管理系统
  • 三个伪类让你的CSS代码更加优雅
  • 幻兽帕鲁联机服务器搭建
  • 京东商品优惠券API获取商品到手价
  • Flutter Version Manager (FVM): Flutter的版本管理终极指南
  • Docker技术概论(3):Docker 中的基本概念
  • 死记硬背spring bean 的生命周期
  • 海外网红营销策略:如何将红人粉丝有效转化为品牌忠实粉丝?
  • java之Bean对象
  • Flink——芒果TV的实时数仓建设实践
  • 卸载云服务器上的 MySQL 数据库
  • AUTOSAR SPI详解