class AreaManager { companion object { @Volatile private var instance: AreaManager? = null @JvmStatic fun getInstance(): AreaManager { return instance ?: synchronized(this) { instance ?: AreaManager().also { instance = it } } } } suspend fun updateArea(context: Context): Boolean { val filePath = downloadFile(context) return when (filePath) { null -> false else -> true } } }
我现在想在 fragment 的 java 代码里调用,应该怎么做?试了 Claude 、GPT 都没能实现,各种飘红。
android 也太难学了
![]() | 2 lisongeee 314 天前 ![]() ```kt suspend fun updateArea(context: Context): Boolean { TODO() } fun syncUpdateArea(context: Context): Boolean { return runBlocking { updateArea(context) } } ``` |
3 location123 314 天前 ![]() 如二楼 java 开一个线程 kt 中使用 runBlocking 调用 suspend 函数 或者 kotlin 多写一个函数转成 callback |
![]() | 4 GotKiCry 314 天前 ![]() 给 Kotlin 再封装一层给 Java 是你最好的选择 |
5 iOCZS 314 天前 ![]() swift 的 await 和 Task 熟悉了吗? |
![]() | 6 rrubick OP 感谢大佬们,可以了。 搜索结果让我再自定义个 CustomContinuation ,然后再实现 resume 啥的方法,完全不知道在哪获取返回结果 @iOCZS #5 惭愧,近几年原生代码写的少很多,swift 5.几以后的 await async 没怎么用了,swiftUI 用了一下坑不少也没怎么实践。目前还是习惯用 delegate 和 block 来处理异步事件。 |
7 yazinnnn0 314 天前 做不到的, 只能在 kotlin 代码里处理成 Future 之类的东西,再扔给 java |
![]() | 8 Achieve7 314 天前 我刚开始学的时候也踩过这个坑, 只能转成 java 的线程处理类 future completed 之类的才行 |
9 lmshl 314 天前 ![]() 1. asCompletableFuture https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.future/ UI 代码里可以用, 非阻塞 2. runBlocking https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/run-blocking.html 非 UI 代码里可以用, 阻塞 建议都用 Kotlin 写, 没啥必要 Java/Kotlin 混写 |
10 guoziq09 314 天前 你甚至不想搜索一下协程这两个字。 fun test(){ CoroutineScope(Dispatchers.Main).launch { testCoroutine() } } suspend fun testCoroutine() = withContext(Dispatchers.IO){ delay(1000) //todo } |
![]() | 11 rrubick OP |
13 e3c78a97e0f8 313 天前 via iPhone @magic3584 OC 调用 Swift 的 async await 也不简单吧,好像是完全不行? |
![]() | 15 rrubick OP @e3c78a97e0f8 #13 不太清楚,async await 之后就没写过桥接代码 |
![]() | 17 mtdhllf 313 天前 AreaManager 要做单例,你直接 object AreaManager{ }就可以了 |
18 fuckshiter 313 天前 我是使用了 future |
![]() | 19 0x0x 313 天前 |
![]() | 20 rrubick OP @ck19920702 #19 请问方法返回的 Bool 在哪取呢 |
![]() | 21 0x0x 313 天前 ![]() @magic3584 #20 public void resumeWith(@NonNull Object o) o 强制转成 Boolean getContext() 返回 EmptyCoroutineContext.INSTANCE |
![]() | 22 Kamiyu0087 308 天前 为啥不直接用 kotlin 写呢? |
![]() | 23 rrubick OP @Kamiyu0087 #22 老项目,java kotlin 都有。而我是新新手,只能就着改改 |
![]() | 24 rrubick OP @lisongeee #2 @location123 #3 @GotKiCry #4 @yazinnnn0 #7 @Achieve7 #8 @lmshl #9 @guoziq09 #10 @zoharSoul #12 @mtdhllf #17 @fuckshiter #18 @ck19920702 #19 @Kamiyu0087 #22 请教各位大佬,怎么快速知道当前是哪个 activity? 我用命令行 ` adb shell dumpsys activity top | grep ACTIVITY` 时好时不好。现在只能搜索当前页面上的字去一个一个找 |
![]() | 26 0x0x 308 天前 ![]() @magic3584 #24 as 里 logcat 过滤下 :message:Displayed ,记得把包名限制去掉。 过滤日志那一行只保留 message:Displayed ,可以看到当前 Activity |
27 location123 308 天前 @magic3584 #24 试下 adb shell dumpsys window | grep Activity 查看 window 上的 activity 我不是专业的 Android 开发 |
![]() | 28 rrubick OP @ck19920702 #26 感谢,有些可用,只不过有一些会打印 ` myPackage.ComposeActivity ( ComposeActivity: ComponentActivity())` @zoharSoul #25 是用 26 楼方法吗? |
![]() | 29 zoharSoul 308 天前 |
![]() | 30 zoharSoul 308 天前 ![]() |