{"id":584,"date":"2024-08-27T11:01:08","date_gmt":"2024-08-27T02:01:08","guid":{"rendered":"https:\/\/txn.myds.me\/blog\/?p=584"},"modified":"2024-08-27T11:01:08","modified_gmt":"2024-08-27T02:01:08","slug":"android-%e3%81%a7%e3%82%b5%e3%83%bc%e3%83%93%e3%82%b9%e3%81%a8%e9%80%9a%e4%bf%a1%e3%81%99%e3%82%8b-channel%e7%b7%a8","status":"publish","type":"post","link":"https:\/\/blog.txn.red\/?p=584","title":{"rendered":"Android \u3067\u30b5\u30fc\u30d3\u30b9\u3068\u901a\u4fe1\u3059\u308b-Channel\u7de8"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u30b5\u30fc\u30d3\u30b9\u3068\u901a\u4fe1\u3059\u308b\u30b5\u30f3\u30d7\u30eb 3 \u90e8\u4f5c\u306e\u8ffd\u52a0\u7de8\u3067\u3059\u3002\u3067 Flow (Channel) \u3092\u4f7f\u7528\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\uff13 \u90e8\u4f5c\u306f\u3053\u3061\u3089<\/p>\n\n\n\n\n\n<p class=\"wp-block-paragraph\">\u305d\u3082\u305d\u3082\u3001\u306a\u305c\u4e0a\u306e\uff13\u624b\u6cd5\u3092\u8a66\u3057\u305f\u306e\u304b\u3068\u3044\u3046\u3068\u3001Claude \u304b Gemini \u304b ChatGTP \u306b\u300c\u30b5\u30fc\u30d3\u30b9\u306e\u72b6\u614b\u3092\u30a2\u30af\u30c6\u30a3\u30d3\u30c6\u30a3\u306b\u901a\u77e5\u3059\u308b\u306b\u306f\u3069\u3046\u3044\u3046\u65b9\u6cd5\u304c\u3042\u308b\uff1f\u300d\u3063\u3066\u805e\u3044\u305f\u7d50\u679c\u30013 \u3064\u306e\u65b9\u6cd5\u304c\u56de\u7b54\u306b\u3042\u3063\u305f\u306e\u304c\u30ad\u30c3\u30ab\u30b1\u3060\u3063\u305f\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u3067\u30b5\u30f3\u30d7\u30eb\u3092\u63a2\u3057\u3066\u4f5c\u3063\u3066\u3044\u304f\u3046\u3061\u306b\u3001Flow \u3092\u30a4\u30d9\u30f3\u30c8\u30d0\u30b9\u306b\u4f7f\u3046\u65b9\u6cd5\u3082\u3042\u308b\u3089\u3057\u3044\u3068\u3044\u3046\u3053\u3068\u3092\u63a2\u3057\u5f53\u3066\u305f\u306e\u3067\u3084\u3063\u3066\u307f\u308b\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">MainActivity \u306f\u3053\u3093\u306a\u611f\u3058\u306b\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-Kotlin\" data-file=\"MainActivity.kt\" data-lang=\"Kotlin\" data-line=\"50-55\"><code>package red.txn.service_flow\n\nimport android.content.ComponentName\nimport android.content.Context\nimport android.content.Intent\nimport android.content.ServiceConnection\nimport android.os.Bundle\nimport android.os.IBinder\nimport android.util.Log\nimport androidx.activity.ComponentActivity\nimport androidx.activity.compose.setContent\nimport androidx.activity.enableEdgeToEdge\nimport androidx.compose.foundation.layout.fillMaxSize\nimport androidx.compose.foundation.layout.padding\nimport androidx.compose.material3.Scaffold\nimport androidx.compose.material3.Text\nimport androidx.compose.runtime.Composable\nimport androidx.compose.ui.Modifier\nimport androidx.compose.ui.tooling.preview.Preview\nimport kotlinx.coroutines.CoroutineScope\nimport kotlinx.coroutines.Dispatchers\nimport kotlinx.coroutines.launch\nimport red.txn.service_flow.ui.theme.ServiceflowTheme\n\nclass MainActivity : ComponentActivity() {\n    companion object {\n        const val TAG = &quot;MainActivity&quot;\n    }\n\n    override fun onCreate(savedInstanceState: Bundle?) {\n        super.onCreate(savedInstanceState)\n        enableEdgeToEdge()\n        setContent {\n            ServiceflowTheme {\n                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding -&gt;\n                    Greeting(\n                        name = &quot;Android&quot;,\n                        modifier = Modifier.padding(innerPadding)\n                    )\n                }\n            }\n        }\n\n        \/\/ launch service\n        Intent(this, FlowService::class.java).also { intent -&gt;\n            startService(intent)\n        }\n        Log.d(TAG,&quot;service launched&quot;)\n\n        val scope = CoroutineScope(Dispatchers.IO)\n        scope.launch {\n            FlowService.BUS.collect {\n                Log.d(TAG, &quot;Received data: $it&quot;)\n            }\n        }\n    }\n}\n\n@Composable\nfun Greeting(name: String, modifier: Modifier = Modifier) {\n    Text(\n        text = &quot;Hello $name!&quot;,\n        modifier = modifier\n    )\n}\n\n@Preview(showBackground = true)\n@Composable\nfun GreetingPreview() {\n    ServiceflowTheme {\n        Greeting(&quot;Android&quot;)\n    }\n}<\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">LiveData \u306e observe \u304c Flow \u306e cllect \u306b\u5909\u308f\u3063\u305f\u611f\u3058\u3067\u6975\u7aef\u306a\u5909\u5316\u306f\u306a\u3044\u3067\u3059\u306d\u3002\u308f\u304b\u308a\u3084\u3059\u3044\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u30b5\u30fc\u30d3\u30b9\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306a\u3057\u3067\u30a2\u30af\u30bb\u30b9\u3067\u304d\u3066\u3044\u308b\u306e\u306f BUS \u304c\u30b5\u30fc\u30d3\u30b9\u306e\u4e2d\u3067 companion ojbect  \u3068\u3057\u3066\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u308b\u304b\u3089\u3067\u3059\u3002\uff08\u7406\u89e3\u3042\u3063\u3066\u308b\uff1f\uff09<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-Kotlin\" data-file=\"FlowService.kt\" data-lang=\"Kotlin\" data-line=\"19-20, 41-44\"><code>package red.txn.service_flow\n\nimport android.app.Service\nimport android.content.Intent\nimport android.os.Binder\nimport android.os.IBinder\nimport android.util.Log\nimport kotlinx.coroutines.CoroutineScope\nimport kotlinx.coroutines.Dispatchers\nimport kotlinx.coroutines.channels.Channel\nimport kotlinx.coroutines.flow.MutableStateFlow\nimport kotlinx.coroutines.flow.asSharedFlow\nimport kotlinx.coroutines.flow.receiveAsFlow\nimport kotlinx.coroutines.launch\n\nclass FlowService : Service() {\n    companion object {\n        val TAG = &quot;FlowService&quot;\n        private var _BUS = Channel&lt;String&gt;()\n        val BUS = _BUS.receiveAsFlow()\n    }\n\n    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {\n        Log.d(TAG, &quot;onBind&quot;)\n        Thread {\n            for (i in 1..2) {\n                Thread.sleep(2000)\n                updateData(&quot;\u30c7\u30fc\u30bf$i&quot;)\n                Log.d(TAG, &quot;\u30c7\u30fc\u30bf$i&quot;)\n            }\n        }.start()\n\n        return START_STICKY\n    }\n\n    override fun onBind(intent: Intent): IBinder? {\n        return null\n    }\n\n    fun updateData(data: String) {\n        val scope = CoroutineScope(Dispatchers.IO)\n        scope.launch {\n            _BUS.send(data)\n        }\n    }\n}<\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">BUS \u3068 _BUS  \u306f companion object \u304b\u3089\u666e\u901a\u306e\u30e1\u30f3\u30d0\u306b\u5909\u3048\u3066\u3082\u3044\u3044\u304b\u3082\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">AI\u30c1\u30e3\u30c3\u30c8\u30dc\u30c3\u30c8\u306e\u56de\u7b54\u3067\u6f0f\u308c\u3066\u3044\u305f\u65b9\u6cd5\u3092\u305f\u307e\u305f\u307e\u898b\u3064\u3051\u305f\u308f\u3051\u3067\u3059\u304c\u3001\u73fe\u6642\u70b9\u3067\u306f AI \u306e\u56de\u7b54\u304c\u7db2\u7f85\u6027\u304c\u3042\u308a\u3001\u6b63\u3057\u3044\uff08\u59a5\u5f53\u304b\uff09\u3092\u5224\u65ad\u3059\u308b\u306b\u306f\u8aad\u307f\u53d6\u308b\u4eba\u9593\u5074\u304c\u305d\u3082\u305d\u3082\u305d\u306e\u7b54\u3048\u77e5\u3089\u306a\u3044\u3068\u3044\u3051\u306a\u3044\u4f8b\u3068\u3057\u3066\u899a\u3048\u3066\u304a\u304d\u305f\u3044\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">AI\u30c1\u30e3\u30c3\u30c8\u30dc\u30c3\u30c8\u306b\u9650\u3089\u305a\u691c\u7d22\u306e\u56de\u7b54\u304b\u3089\u6f0f\u308c\u3066\u3044\u308b\u3082\u306e\u304c\u8a8d\u8b58\u3055\u308c\u306a\u3044\u3001\u3064\u307e\u308a\u5b58\u5728\u3057\u306a\u3044\u3053\u3068\u306b\u3055\u308c\u3066\u3057\u307e\u3046\u306e\u306f\u3068\u3066\u3082\u8003\u3048\u3055\u305b\u3089\u308c\u308b\u3068\u611f\u3058\u3066\u3057\u307e\u3046\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"%25e5%258f%2582%25e8%2580%2583\">\u53c2\u8003<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/qiita.com\/yasukotelin\/items\/3a6afe861cc9345118d6\">LiveData\u3092Flow\u306b\u30ea\u30d7\u30ec\u30fc\u30b9\u3057\u3066\u307f\u3066\u5f97\u305f\u77e5\u898b\uff08StateFlow\u3001SharedFlow\u3001Channel\uff09 #Android &#8211; Qiita<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u30b5\u30fc\u30d3\u30b9\u3068\u901a\u4fe1\u3059\u308b\u30b5\u30f3\u30d7\u30eb 3 \u90e8\u4f5c\u306e\u8ffd\u52a0\u7de8\u3067\u3059\u3002\u3067 Flow (Channel) \u3092\u4f7f\u7528\u3057\u3066\u307f\u307e\u3059\u3002 \uff13 \u90e8\u4f5c\u306f\u3053\u3061\u3089 \u305d\u3082\u305d\u3082\u3001\u306a\u305c\u4e0a\u306e\uff13\u624b\u6cd5\u3092\u8a66\u3057\u305f\u306e\u304b\u3068\u3044\u3046\u3068\u3001Claude \u304b Gemini \u304b ChatGTP  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":0,"footnotes":""},"categories":[9],"tags":[4],"class_list":["post-584","post","type-post","status-publish","format-standard","hentry","category-dev","tag-android"],"_links":{"self":[{"href":"https:\/\/blog.txn.red\/index.php?rest_route=\/wp\/v2\/posts\/584","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.txn.red\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.txn.red\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.txn.red\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.txn.red\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=584"}],"version-history":[{"count":0,"href":"https:\/\/blog.txn.red\/index.php?rest_route=\/wp\/v2\/posts\/584\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.txn.red\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.txn.red\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.txn.red\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}