基于Kotlin Multiplatform实现静态文件服务器(四)

2024-08-23 23:36

本文主要是介绍基于Kotlin Multiplatform实现静态文件服务器(四),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Android前台服务

Android前台服务用于执行用户可察觉的操作。

前台服务显示状态栏通知,让用户知道您的 应用正在前台执行任务并消耗系统资源。

文件服务启动和停止

在JVM上,通过守护线程来启动文件服务。

class DesktopServerViewModel : ServerViewModel, ViewModel() {...override fun startFileServer(httpServerConfig: HttpFileServerConfig?) {startServerDaemon(httpServerConfig)}override fun stopFileServer() {stopServerDaemon()}
}actual fun getMsgViewModel(): ServerViewModel = DesktopServerViewModel()
class ServerDaemon(private val httpServerConfig: HttpFileServerConfig) : Thread() {override fun run() {bootStrapServer.startServer(httpServerConfig)}companion object {private lateinit var serverDaemon: ServerDaemonfun startServerDaemon(httpServerConfig: HttpFileServerConfig?) {httpServerConfig ?: run {LogTool.e("不能加载配置文件")return}serverDaemon = ServerDaemon(httpServerConfig).also {it.isDaemon = trueit.name = "Server-daemon-thread"}.apply {start()}}fun stopServerDaemon() {try {bootStrapServer.stopServer()if (serverDaemon.isAlive) {serverDaemon.interrupt()}} catch (e: InterruptedException) {e.printStackTrace()}}}
}

在Android中,为了避免由于界面退出或锁屏后被系统杀死进程导致文件服务中断,使用前台服务实现。

@Suppress("DEPRECATION")
class AndroidServerViewModel : ViewModel(), ServerViewModel {...@RequiresApi(VERSION_CODES.O)override fun startFileServer(httpServerConfig: HttpFileServerConfig?) {FileServerApp.app.startForegroundService(fileServerIntent.also { //启动前台服务it.putExtra("serverPort", httpServerConfig?.serverPort)it.putExtra("fileDirectory", httpServerConfig?.fileDirectory)})}override fun stopFileServer() {FileServerApp.app.stopService(fileServerIntent) // 停止前台服务。}...private companion object {...val fileServerIntent = Intent().also {it.setClass(FileServerApp.app.applicationContext, FileServerService::class.java)}}
}actual fun getMsgViewModel(): ServerViewModel = AndroidServerViewModel()

创建FileServerService,并声明为前台服务。声明前台服务,需要在AndroidManifest.xml中声明,并申请权限。 权限相关可参阅https://blog.csdn.net/luckyion/article/details/141305213

class FileServerService : Service() {override fun onBind(intent: Intent): IBinder? = null@RequiresApi(Build.VERSION_CODES.O)override fun onCreate() {super.onCreate()getSystemService(NotificationManager::class.java).apply {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {createNotificationChannel(NotificationChannel(MY_CHANNEL_ID,MY_CHANNEL_NAME,NotificationManager.IMPORTANCE_HIGH))}}try {startForeground(MY_NOTIFICATION_ID, getNotification(this))LogTool.i("startForeground")} catch (e: Exception) {e.printStackTrace()}}override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {LogTool.i("onStartCommand")startServer(HttpFileServerConfig(intent?.getIntExtra("serverPort", 80)!!,intent.getStringExtra("fileDirectory")!!))return super.onStartCommand(intent, flags, startId)}private fun startServer(httpServerConfig: HttpFileServerConfig) {LogTool.i("startServer")startServerDaemon(httpServerConfig) // 启动文件服务线程}private fun stopServer() {stopServerDaemon()  // 停止文件服务线程}override fun onDestroy() {stopServer()super.onDestroy()}@RequiresApi(Build.VERSION_CODES.O)private fun getNotification(context: Context) = // 前台服务通知,创建前台服务,必须发送一个通知。Notification.Builder(context, MY_CHANNEL_ID).setShowWhen(true).setSmallIcon(R.drawable.explore).setContentTitle("服务服务器").setContentText("服务已启动").also {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {it.setChannelId(MY_CHANNEL_ID)}}.build()private companion object {private const val MY_CHANNEL_ID = "File-server-foreground-service-channel"private const val MY_CHANNEL_NAME = "File-server-foreground-service-channel-name"private const val MY_NOTIFICATION_ID = 10086}
}

这篇关于基于Kotlin Multiplatform实现静态文件服务器(四)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1100816

相关文章

Python实现终端清屏的几种方式详解

《Python实现终端清屏的几种方式详解》在使用Python进行终端交互式编程时,我们经常需要清空当前终端屏幕的内容,本文为大家整理了几种常见的实现方法,有需要的小伙伴可以参考下... 目录方法一:使用 `os` 模块调用系统命令方法二:使用 `subprocess` 模块执行命令方法三:打印多个换行符模拟

SpringBoot+EasyPOI轻松实现Excel和Word导出PDF

《SpringBoot+EasyPOI轻松实现Excel和Word导出PDF》在企业级开发中,将Excel和Word文档导出为PDF是常见需求,本文将结合​​EasyPOI和​​Aspose系列工具实... 目录一、环境准备与依赖配置1.1 方案选型1.2 依赖配置(商业库方案)二、Excel 导出 PDF

Python实现MQTT通信的示例代码

《Python实现MQTT通信的示例代码》本文主要介绍了Python实现MQTT通信的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 安装paho-mqtt库‌2. 搭建MQTT代理服务器(Broker)‌‌3. pytho

SpringBoot改造MCP服务器的详细说明(StreamableHTTP 类型)

《SpringBoot改造MCP服务器的详细说明(StreamableHTTP类型)》本文介绍了SpringBoot如何实现MCPStreamableHTTP服务器,并且使用CherryStudio... 目录SpringBoot改造MCP服务器(StreamableHTTP)1 项目说明2 使用说明2.1

使用zip4j实现Java中的ZIP文件加密压缩的操作方法

《使用zip4j实现Java中的ZIP文件加密压缩的操作方法》本文介绍如何通过Maven集成zip4j1.3.2库创建带密码保护的ZIP文件,涵盖依赖配置、代码示例及加密原理,确保数据安全性,感兴趣的... 目录1. zip4j库介绍和版本1.1 zip4j库概述1.2 zip4j的版本演变1.3 zip4

Kotlin Map映射转换问题小结

《KotlinMap映射转换问题小结》文章介绍了Kotlin集合转换的多种方法,包括map(一对一转换)、mapIndexed(带索引)、mapNotNull(过滤null)、mapKeys/map... 目录Kotlin 集合转换:map、mapIndexed、mapNotNull、mapKeys、map

python生成随机唯一id的几种实现方法

《python生成随机唯一id的几种实现方法》在Python中生成随机唯一ID有多种方法,根据不同的需求场景可以选择最适合的方案,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习... 目录方法 1:使用 UUID 模块(推荐)方法 2:使用 Secrets 模块(安全敏感场景)方法

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

Spring Boot 结合 WxJava 实现文章上传微信公众号草稿箱与群发

《SpringBoot结合WxJava实现文章上传微信公众号草稿箱与群发》本文将详细介绍如何使用SpringBoot框架结合WxJava开发工具包,实现文章上传到微信公众号草稿箱以及群发功能,... 目录一、项目环境准备1.1 开发环境1.2 微信公众号准备二、Spring Boot 项目搭建2.1 创建

IntelliJ IDEA2025创建SpringBoot项目的实现步骤

《IntelliJIDEA2025创建SpringBoot项目的实现步骤》本文主要介绍了IntelliJIDEA2025创建SpringBoot项目的实现步骤,文中通过示例代码介绍的非常详细,对大家... 目录一、创建 Spring Boot 项目1. 新建项目2. 基础配置3. 选择依赖4. 生成项目5.