Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Nativeコードからの値をStateFlowで受け取ってみた
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
kazaman
December 22, 2020
Programming
1.6k
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Nativeコードからの値をStateFlowで受け取ってみた
kazaman
December 22, 2020
Other Decks in Programming
See All in Programming
AWS CDKのカスタムリソースでContinuum(旧Security Agent)を実装した話 | Implementing Continuum (formerly Security Agent) via AWS CDK Custom Resource
akihisaikeda
2
170
Zero-Tap Sign-In対応、結局何をすればいいの?
mitohato14
0
710
APNsからLive Activityを開始する話
yumnumm
0
180
dotNET_vulnclicker_intro.pdf
deka_morita
0
150
Coding with AI: How to trust the storyteller (AWS Community Day Adria, Ljubljana 2026)
slobodan
0
120
Kotlinの15年とServer-Side Kotlin
n_takehata
1
160
golang-bob.pdf
iwatsukayura
0
140
FactoryBotをやめればRSpecは速くなるのか?
yuta_onishi_97
0
1.1k
[Dev Group Paris Octobre 26] Agentforce, SIforce, Claudeforce, Slackbot… on s'y retrouve ?
nabondance
0
120
contenteditable と日本語入力に向き合う
colorful12
0
220
Jetpack Compose Mechanisms
skydoves
3
870
Snowflake Openflow Connector利用の勘所 / 20261024developersio2026tokyo-snowflake-openflow
kasacchiful
PRO
0
160
Featured
See All Featured
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
128
56k
Mobile First: as difficult as doing things right
swwweet
225
10k
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
650
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
1
290
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
380
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.9k
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
330
Being A Developer After 40
akosma
PRO
92
590k
Game over? The fight for quality and originality in the time of robots
wayneb77
1
310
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
620
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2.5k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
6.1k
Transcript
Nativeコードからの値を StateFlowで受け取ってみた potatotips #72 2020/12/22(Tue) kazaman97
自己紹介 kazaman 株式会社ゼネテック : 社会人 1 年目 Android ハマっている! でも歴としては、空いてる期間もあるけど
1 年くらい 最近業務の方でも行うようになった potatotips 初参加にして初発表なので温かい目で、、、 github: @kazaman97
なんでこの議題なの?? 業務で NDK を使い Native コードを Android 上で動かす必要があった Native 側では、処理が終わるまで常に動作させており、結果を出力し続ける
その結果を受け取り、画面に表示させなければならない! しかし、 ( 自分が ) やり方を見つけられなかったで自分なりに行ってみた!
(Android Dagashi より [ https://androiddagashi.github.io/issue/147-2020-11-22/ ]) なにかいい方法がないかと 探していたら見つけた記事 このStateFlowを 使えばうまいこと
できそう??
StateFlow StateFlow は、状態保持用の監視可能な Flow で、現在の状態や状態更新の情報をコレ クタに出力します。 (https://developer.android.com/kotlin/flow/stateflow-and-sharedflow より引用 ) StateFlowを使えばNativeからの最新値を常に受け取れそう!
StateFlowの使い方 1 2 3 1. 外からは値の変更をできないようにし、 更新された値を流せるようにするため、 privateでMutableStateFlowを定義する 2. 外からは読み取り専用でアクセスできるようにするため、
StateFlowにする 3. 値を発行させる箇所に、 定義したMutableStateFlowのvalueに値を入れる 初期値が必要!
サンプルで作成したアプリ 1. EditText にカウントダウンしたい 数値を入れる 2. 「スタート」ボタンを押したら 入力した数値が TextView に表示さ
れ、 1 秒ずつカウントダウンを開始する 3. 0 もしくは「ストップ」ボタンを押したらカ ウントダウンが終了する カウントダウンアプリ画面 カウントダウン処理を Nativeで行わせる https://github.com/kazaman97/sample-jni-flow
Nativeコード 1 秒ずつカウントダウンする C++ コード 1. start 関数を呼んだら カウントダウン処理を実行する 2.
カウントダウン処理時中に 現在のタイムを出力し続ける 3. カウント処理が 0 になる、もしくは stop 関 数を呼んだら止まる スタート処理 ストップ処理 カウント ダウン処理 JVM側に値を 渡す処理
Nativeの関数呼び出し `start` 関数を呼び出すと Native 側のコードが呼び出される `onUpdateTime` は Native 側から、 現在のカウントダウン値を渡すときに呼ば
れる `currentTime` で 今回 UI 側に値渡しする StateFlow Native 実装やり取りをするためのコード Nativeからの値 を受け取る
①. JNI(Nativeコードとマネージ コードを連携するインターフェース ) を使ってJVM側に 値を渡す 値を渡す ②. MutableStateFlowに値を渡す ③.
値を受け取るときは 使いたい箇所でcollectするだけ!
UI側での処理 コルーチンを作成し、その中で、 collect す る collect して受け取った値を TextView に セットする
1 秒ずつカウントダウンする C++ コード
実際の動作 実際に動かしてみると、 Native で処理したカウントダウン値がしっか り流れてきている!
まとめ StateFlow を使うと Native 側の値を簡単に受け取り UI 反映ができる コルーチンを使っているのでバックグラウンドスレッドで値受け取りもできる 業務の方は、まだ Listener
方式で直していない
ご清聴ありがとうございました♂