{"id":188,"date":"2023-12-15T15:26:49","date_gmt":"2023-12-15T07:26:49","guid":{"rendered":"https:\/\/www.jishuge.cn\/?p=188"},"modified":"2023-12-15T15:33:34","modified_gmt":"2023-12-15T07:33:34","slug":"jetpack-room-%e5%85%a5%e9%97%a8%e4%bd%bf%e7%94%a8%e5%ae%8c%e6%95%b4%e7%a4%ba%e4%be%8b","status":"publish","type":"post","link":"https:\/\/blog.jishuge.cn\/?p=188","title":{"rendered":"JetPack Room \u5165\u95e8\u4f7f\u7528\u5b8c\u6574\u793a\u4f8b"},"content":{"rendered":"\n<p>\u5177\u4f53\u6570\u636e\u53ef\u4ee5\u53c2\u8003\u5b98\u65b9\u6587\u6863\uff1a<a href=\"https:\/\/developer.android.com\/codelabs\/kotlin-android-training-room-database?hl=zh-cn#0\" data-type=\"link\" data-id=\"https:\/\/developer.android.com\/codelabs\/kotlin-android-training-room-database?hl=zh-cn#0\">https:\/\/developer.android.com\/codelabs\/kotlin-android-training-room-database?hl=zh-cn#0<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u7b2c\u4e00\u6b65\uff1a\u5bfc\u5165\u4f9d\u8d56<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1\u3001\u63d2\u4ef6<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">plugins {\n    id(\"com.android.application\")\n    id(\"org.jetbrains.kotlin.android\")\n    id(\"kotlin-kapt\")\n}<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">2\u3001\u4f9d\u8d56<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"kotlin\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">implementation(\"androidx.room:room-runtime:2.6.1\")\nannotationProcessor(\"androidx.room:room-compiler:2.6.1\")\nimplementation(\"androidx.room:room-ktx:2.6.1\")\nkapt(\"androidx.room:room-compiler:2.6.1\")<\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u7248\u672c\u95ee\u9898\uff08\u53ef\u80fd\u5b58\u5728\uff09<\/h5>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    compileOptions {\n        \/\/JavaVersion.VERSION_1_8\u8fd9\u4e2a\u7248\u672c\u53ef\u80fd\u4e0d\u517c\u5bb9\uff0c\u6839\u636e\u73af\u5883\u7684\u7248\u672c\u6765\u66f4\u6362\n        sourceCompatibility = JavaVersion.VERSION_17 \n        targetCompatibility = JavaVersion.VERSION_17\n    }\n    kotlinOptions {\n        jvmTarget = \"17\"\n    }<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e8c\u3001\u5b9a\u4e49\u5b9e\u4f53\u7c7b<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import androidx.room.ColumnInfo\nimport androidx.room.Entity\nimport androidx.room.PrimaryKey\n\n@Entity(tableName = \"daily_sleep_quality_table\")\ndata class SleepNight(\n\n    @PrimaryKey(autoGenerate = true)\n    var nightId: Long = 0L,\n\n    @ColumnInfo(name = \"start_time_milli\")\n    val startTimeMilli: Long = System.currentTimeMillis(),\n\n    @ColumnInfo(name = \"end_time_milli\")\n    var endTimeMilli: Long = startTimeMilli,\n\n    @ColumnInfo(name = \"quality_rating\")\n    var sleepQuality: Int = -1\n)\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e09\u3001\u5b9a\u4e49Dao\u8bbf\u95ee\u7c7b<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import androidx.lifecycle.LiveData\nimport androidx.room.Dao\nimport androidx.room.Insert\nimport androidx.room.Query\nimport androidx.room.Update\n\n@Dao\ninterface SleepDatabaseDao {\n\n    @Insert\n    fun insert(night: SleepNight)\n\n    @Update\n    fun update(night: SleepNight)\n\n    @Query(\"SELECT * from daily_sleep_quality_table WHERE nightId = :key\")\n    fun get(key: Long): SleepNight?\n\n\n    @Query(\"DELETE FROM daily_sleep_quality_table\")\n    fun clear()\n\n    @Query(\"SELECT * FROM daily_sleep_quality_table ORDER BY nightId DESC LIMIT 1\")\n    fun getTonight(): SleepNight?\n\n    @Query(\"SELECT * FROM daily_sleep_quality_table ORDER BY nightId DESC\")\n    fun getAllNights(): LiveData&lt;List&lt;SleepNight>>\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u56db\u3001\u5b9a\u4e49DataBase\u64cd\u4f5c\u7c7b<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import android.content.Context\nimport androidx.room.Database\nimport androidx.room.Room\nimport androidx.room.RoomDatabase\n\n@Database(entities = [SleepNight::class], version = 1, exportSchema = false)\nabstract class SleepDatabase : RoomDatabase() {\n\n    abstract val sleepDatabaseDao: SleepDatabaseDao\n\n    companion object {\n\n        @Volatile\n        private var INSTANCE: SleepDatabase? = null\n\n        fun getInstance(context: Context): SleepDatabase {\n            synchronized(this) {\n                var instance = INSTANCE\n\n                if (instance == null) {\n                    instance = Room.databaseBuilder(\n                        context.applicationContext,\n                        SleepDatabase::class.java,\n                        \"sleep_history_database\"\n                    )\n                        .fallbackToDestructiveMigration()\n                        .build()\n                    INSTANCE = instance\n                }\n                return instance\n            }\n        }\n    }\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e94\u3001\u4f7f\u7528\uff08\u5fc5\u987b\u5728\u5b50\u7ebf\u7a0b\u4e2d\u6216\u8005\u534f\u7a0b\u4e2d\u4f7f\u7528\uff09<\/h3>\n\n\n\n<p>\u5728\u9879\u76ee\u4e2d\u6211\u4eec\u4e00\u822c\u662f\u7ed3\u5408LiveData\u548cViewModel\u4e00\u8d77\u4f7f\u7528\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class MainActivity2 : ComponentActivity() {\n    override fun onCreate(savedInstanceState: Bundle?) {\n        super.onCreate(savedInstanceState)\n\n        setContent {\n            MyApplicationTheme {\n                GetMessage(this)\n            }\n        }\n    }\n}\n\n@Composable\nfun GetMessage(mainActivity2: MainActivity2) {\n    Column {\n        Button(onClick = {\n            GlobalScope.launch{\n                try {\n                    SleepDatabase.getInstance(mainActivity2)\n                        .sleepDatabaseDao\n                        .insert(SleepNight())\n                }catch (e:Exception){\n                    Log.e(\"Xiang\",\"\u51fa\u73b0\u4e86\u9519\u8bef\uff1a${e.message}\")\n                }\n            }\n        }) {\n            Text(text = \"\u6dfb\u52a0\u6570\u636e\")\n        }\n    }\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u516d\u3001\u5229\u7528Android Studio\u5de5\u5177\u67e5\u770b<\/h3>\n\n\n\n<p>\u6253\u5f00\u83dc\u5355\u3010view\u3011-&gt;\u3010Tool Windows\u3011-&gt;\u3010App Inspection\u3011<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"224\" src=\"https:\/\/cdn.piwiki.cn\/jishuge\/2023\/12\/20231215072528669-1024x224.png\" alt=\"\" class=\"wp-image-192\" srcset=\"https:\/\/cdn.piwiki.cn\/jishuge\/2023\/12\/20231215072528669-1024x224.png 1024w, https:\/\/cdn.piwiki.cn\/jishuge\/2023\/12\/20231215072528669-300x66.png 300w, https:\/\/cdn.piwiki.cn\/jishuge\/2023\/12\/20231215072528669-768x168.png 768w, https:\/\/cdn.piwiki.cn\/jishuge\/2023\/12\/20231215072528669-1536x336.png 1536w, https:\/\/cdn.piwiki.cn\/jishuge\/2023\/12\/20231215072528669-2048x448.png 2048w, https:\/\/cdn.piwiki.cn\/jishuge\/2023\/12\/20231215072528669-640x140.png 640w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u5177\u4f53\u6570\u636e\u53ef\u4ee5\u53c2\u8003\u5b98\u65b9\u6587\u6863\uff1ahttps:\/\/developer.android.com\/codelabs\/kot &hellip; <a href=\"https:\/\/blog.jishuge.cn\/?p=188\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">JetPack Room \u5165\u95e8\u4f7f\u7528\u5b8c\u6574\u793a\u4f8b<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":159,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19,23],"tags":[],"class_list":["post-188","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android","category-kotlin"],"_links":{"self":[{"href":"https:\/\/blog.jishuge.cn\/index.php?rest_route=\/wp\/v2\/posts\/188","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.jishuge.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.jishuge.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jishuge.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jishuge.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=188"}],"version-history":[{"count":2,"href":"https:\/\/blog.jishuge.cn\/index.php?rest_route=\/wp\/v2\/posts\/188\/revisions"}],"predecessor-version":[{"id":194,"href":"https:\/\/blog.jishuge.cn\/index.php?rest_route=\/wp\/v2\/posts\/188\/revisions\/194"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.jishuge.cn\/index.php?rest_route=\/wp\/v2\/media\/159"}],"wp:attachment":[{"href":"https:\/\/blog.jishuge.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jishuge.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jishuge.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}