{"id":550,"date":"2024-08-20T11:50:10","date_gmt":"2024-08-20T02:50:10","guid":{"rendered":"https:\/\/txn.myds.me\/blog\/?p=550"},"modified":"2024-08-20T11:50:10","modified_gmt":"2024-08-20T02:50:10","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-%e3%83%96%e3%83%ad%e3%83%bc%e3%83%89%e3%82%ad%e3%83%a3%e3%82%b9%e3%83%88%e7%b7%a8","status":"publish","type":"post","link":"https:\/\/blog.txn.red\/?p=550","title":{"rendered":"Android \u3067\u30b5\u30fc\u30d3\u30b9\u3068\u901a\u4fe1\u3059\u308b-\u30d6\u30ed\u30fc\u30c9\u30ad\u30e3\u30b9\u30c8\u7de8"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u4eca\u56de\u306f3\u90e8\u4f5c\u306b\u306a\u308a\u3001\u305d\u306e\uff11\u3067\u3059\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f55\u3092\u3057\u305f\u3044\u304b\u3068\u3044\u3046\u3068\u30b5\u30fc\u30d3\u30b9\u3067\u4f55\u304b\u5909\u66f4\u3057\u305f\u72b6\u614b\u3092Activity\u306b\u77e5\u3089\u305b\u308b\u65b9\u6cd5\u3092\u8a66\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Gemini \u306b\u805e\u3044\u3066\u307f\u308b\u3068 3 \u3064\u306e\u65b9\u6cd5\u304c\u3042\u308b\u3088\u3046\u3067\u3059\u3002<\/p>\n\n\n\n\n\n<p class=\"wp-block-paragraph\">\u307e\u305a\u306f\u3001SendBroadCast \u3092\u4f7f\u3063\u3066\u307f\u307e\u3059\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u30a4\u30f3\u30c6\u30f3\u30c8\u3092\u30d6\u30ed\u30fc\u30c9\u30ad\u30e3\u30b9\u30c8\u3059\u308b\u30d1\u30bf\u30fc\u30f3\u306e\u5834\u5408\u3001\u4ed6\u306e\u30a2\u30d7\u30ea\u3067\u3082\u53d7\u4fe1\u3067\u304d\u308b\u3088\u3046\u3067\u3059\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u30a2\u30af\u30c6\u30a3\u30d3\u30c6\u30a3\u306f\u3053\u3093\u306a\u611f\u3058\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-Kotlin\" data-file=\"MainActivity.k\uff54\" data-lang=\"Kotlin\" data-line=\"41-43, 55-72\"><code>package red.txn.service_broadcast\n\nimport android.content.BroadcastReceiver\nimport android.content.Context\nimport android.content.Intent\nimport android.content.IntentFilter\nimport android.os.Bundle\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.platform.LocalContext\nimport androidx.compose.ui.tooling.preview.Preview\nimport red.txn.service_broadcast.ui.theme.ServicebroadcastTheme\n\n\nclass MainActivity : ComponentActivity() {\n\n    private lateinit var receiver: MyReceiver\n\n    override fun onCreate(savedInstanceState: Bundle?) {\n        super.onCreate(savedInstanceState)\n        enableEdgeToEdge()\n        setContent {\n            ServicebroadcastTheme {\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        val filter = IntentFilter(&quot;serviceComm&quot;)\n        receiver = MyReceiver()\n        registerReceiver(receiver, filter, RECEIVER_NOT_EXPORTED)\n\n        val serviceIntent = Intent(this, MyService::class.java)\n        startService(serviceIntent)\n    }\n\n    override fun onDestroy() {\n        super.onDestroy()\n        unregisterReceiver(receiver)\n    }\n}\n\nclass MyReceiver: BroadcastReceiver() {\n    companion object {\n        val TAG = &quot;MyReceiver&quot;\n    }\n\n    override fun onReceive(context: Context?, intent: Intent) {\n        val action = intent.action\n        val extras = intent.extras\n        Log.d(TAG, &quot;onReceive - action: $action&quot;)\n\n        extras?.let {\n            for (key in it.keySet()) {\n                val value = it.getString(key)\n                Log.d(TAG, &quot;onReceive - extras: $value&quot;)\n            }\n        }\n    }\n}\n\n@Composable\nfun Greeting(name: String, modifier: Modifier = Modifier) {\n    val context = LocalContext.current\n    Text(\n        text = &quot;Hello $name!&quot;,\n        modifier = modifier\n    )\n}\n\n@Preview(showBackground = true)\n@Composable\nfun GreetingPreview() {\n    ServicebroadcastTheme {\n        Greeting(&quot;Android&quot;)\n    }\n}<\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u30af\u30e9\u30b9 MyReceiver \u306f\u5206\u3051\u305f\u307b\u3046\u304c\u3044\u3044\u3067\u3059\u3051\u3069\u3001\u9762\u5012\u306a\u306e\u3067\u3053\u3053\u306b\u5165\u308c\u3066\u307e\u3059\u3002 IntentFilter \u306b\u76f4\u63a5\u6587\u5b57\u5217\u3067 &#8220;serviceComm&#8221; \u3092\u5165\u308c\u3066\u3044\u307e\u3059\u304c\u3001\u3053\u308c\u306f\u3061\u3083\u3093\u3068 const \u306b\u3057\u305f\u307b\u3046\u304c\u3044\u3044\u3068\u601d\u3044\u307e\u3059\u3002\u3053\u306e\u5024\u306f\u30b5\u30fc\u30d3\u30b9\u5074\u3067\u3082\u540c\u3058\u3082\u306e\u3092\u4f7f\u3044\u9001\u4fe1\u5143\u3092\u8b58\u5225\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u30b5\u30fc\u30d3\u30b9\u306f\u3001Android Studio \u306eFile > New > Service > Service \u30e1\u30cb\u30e5\u30fc\u304b\u3089\u8ffd\u52a0\u3059\u308b\u3068\u7c21\u5358\u3067\u3044\u3044\u3067\u3059\u3002<br>AndroidManifest.xml \u306b\u5fc5\u8981\u306a\u30a8\u30f3\u30c8\u30ea\u3092\u8ffd\u52a0\u3057\u3066\u304f\u308c\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-Kotlin\" data-file=\"MyService.kt\" data-lang=\"Kotlin\" data-line=\"22-23\"><code>package red.txn.service_broadcast\n\nimport android.app.Service\nimport android.content.Intent\nimport android.content.res.Configuration\nimport android.os.IBinder\nimport android.util.Log\n\nclass MyService : Service() {\n    companion object {\n        val TAG = &quot;MyService&quot;\n    }\n\n    override fun onBind(intent: Intent): IBinder? {\n        return null\n    }\n\n    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {\n        Thread {\n            for (i in 1..2) {\n                Thread.sleep(3000)\n                val intent = Intent(&quot;serviceComm&quot;).putExtra(&quot;data&quot;, &quot;\u30e1\u30c3\u30bb\u30fc\u30b8$i&quot;)\n                sendBroadcast(intent)\n                Log.d(TAG, &quot;sendBroadcast: $i&quot;)\n            }\n        }.start()\n\n        return START_STICKY\n    }\n\n    override fun onDestroy() {\n        super.onDestroy()\n        Log.d(TAG, &quot;onDestroy&quot;)\n    }\n}<\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">startService() \u3067\u30b5\u30fc\u30d3\u30b9\u3092\u8d77\u52d5\u3057\u3066\u3044\u308b\u306e\u3067 onStartCommand() \u3067\u53d7\u3051\u307e\u3059\u3002\u4e00\u5fdc\u30b5\u30fc\u30d3\u30b9\u3063\u307d\u304f\u6642\u9593\u304c\u304b\u304b\u308b\u51e6\u7406\u3092\u3059\u308b\u305f\u3081\u306b\u30c0\u30df\u30fc\u3067 Thread {} \u3067\u6642\u9593\u7a3c\u3044\u3067\u307e\u3059\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b9f\u884c\u3059\u308b\u3068 class MyReceiver: BroadcastReceiver() \u3067\u53d7\u4fe1\u3057\u3066\u3001Log.d(TAG, &#8220;onReceive &#8211; extras: $value&#8221;) \u3067\u5185\u5bb9\u3092\u8868\u793a\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6700\u5f8c\u306b AndroidManifest.xml \u3092\u63d0\u793a\u3057\u307e\u3059\u304c\u3001Android Studio \u304c\u81ea\u52d5\u751f\u6210\u3057\u305f\u3082\u306e\u3067\u3044\u3058\u3063\u3066\u3044\u307e\u305b\u3093\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-xml\" data-file=\"AndroidManifest.xml\" data-lang=\"XML\"><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\n&lt;manifest xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;\n    xmlns:tools=&quot;http:\/\/schemas.android.com\/tools&quot;&gt;\n\n    &lt;application\n        android:allowBackup=&quot;true&quot;\n        android:dataExtractionRules=&quot;@xml\/data_extraction_rules&quot;\n        android:fullBackupContent=&quot;@xml\/backup_rules&quot;\n        android:icon=&quot;@mipmap\/ic_launcher&quot;\n        android:label=&quot;@string\/app_name&quot;\n        android:roundIcon=&quot;@mipmap\/ic_launcher_round&quot;\n        android:supportsRtl=&quot;true&quot;\n        android:theme=&quot;@style\/Theme.Servicebroadcast&quot;\n        tools:targetApi=&quot;31&quot;&gt;\n        &lt;service\n            android:name=&quot;.MyService&quot;\n            android:enabled=&quot;true&quot;\n            android:exported=&quot;true&quot;&gt;&lt;\/service&gt;\n\n        &lt;activity\n            android:name=&quot;.MainActivity&quot;\n            android:exported=&quot;true&quot;\n            android:label=&quot;@string\/app_name&quot;\n            android:theme=&quot;@style\/Theme.Servicebroadcast&quot;&gt;\n            &lt;intent-filter&gt;\n                &lt;action android:name=&quot;android.intent.action.MAIN&quot; \/&gt;\n\n                &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; \/&gt;\n            &lt;\/intent-filter&gt;\n        &lt;\/activity&gt;\n    &lt;\/application&gt;\n&lt;\/manifest&gt;<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u56de\u306f3\u90e8\u4f5c\u306b\u306a\u308a\u3001\u305d\u306e\uff11\u3067\u3059\u3002 \u4f55\u3092\u3057\u305f\u3044\u304b\u3068\u3044\u3046\u3068\u30b5\u30fc\u30d3\u30b9\u3067\u4f55\u304b\u5909\u66f4\u3057\u305f\u72b6\u614b\u3092Activity\u306b\u77e5\u3089\u305b\u308b\u65b9\u6cd5\u3092\u8a66\u3057\u3066\u307f\u307e\u3059\u3002 Gemini \u306b\u805e\u3044\u3066\u307f\u308b\u3068 3 \u3064\u306e\u65b9\u6cd5\u304c\u3042\u308b\u3088\u3046\u3067\u3059\u3002 \u307e\u305a\u306f\u3001SendBroadCa [&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":1,"footnotes":""},"categories":[9],"tags":[4],"class_list":["post-550","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\/550","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=550"}],"version-history":[{"count":0,"href":"https:\/\/blog.txn.red\/index.php?rest_route=\/wp\/v2\/posts\/550\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.txn.red\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=550"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.txn.red\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=550"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.txn.red\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=550"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}