{"id":180,"date":"2017-11-10T20:07:14","date_gmt":"2017-11-10T12:07:14","guid":{"rendered":"https:\/\/www.ray650128.com\/wordpress\/?p=180"},"modified":"2021-08-22T14:25:57","modified_gmt":"2021-08-22T06:25:57","slug":"androidapplication%e7%89%a9%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/blog.ray650128.com\/?p=180","title":{"rendered":"[Android]Application\u5143\u4ef6"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">Application\u985e\u5225<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">\u4ec0\u9ebc\u662fApplication?<\/h2>\n\n\n\n<p>Application\u548cActivity\u3001Service\u4e00\u6a23\uff0c\u662fAndroid\u6846\u67b6\u4e2d\u7684\u4e00\u500b\u7cfb\u7d71\u7d44\u4ef6\u3002<\/p>\n\n\n\n<p>\u7576APP\u555f\u52d5\u6642\uff0c\u7cfb\u7d71\u6703\u81ea\u52d5\u5efa\u7acb\u4e00\u500bApplication\u5c0d\u8c61\uff0c\u7528\u4f86\u5132\u5b58\u7cfb\u7d71\u7684\u4e00\u4e9b\u8cc7\u8a0a\u3002<\/p>\n\n\n\n<p>Android\u6703\u5728APP\u57f7\u884c\u6642\uff0c\u66ff\u6bcf\u500bAPP\u5efa\u7acb\u4e00\u500bApplication\u985e\u5225\u7684\u5c0d\u8c61\uff0c\u9019\u500b\u5c0d\u8c61\u53ea\u6703\u5b58\u5728\u4e00\u500b\u5be6\u4f8b\uff0c\u4e5f\u5c31\u662f\u8aaaApplication\u662f\u55ae\u4f8b\u6a21\u5f0f\u7684\u4e00\u500b\u7269\u4ef6\u3002<\/p>\n\n\n\n<p>\u4e00\u822c\u4f86\u8aaa\u6211\u5011\u662f\u4e0d\u9700\u8981\u81ea\u884c\u6307\u5b9a\u4e00\u500bApplication\u7269\u4ef6\u7684\u3002\u4f46\u5982\u679c\u67d0\u4e9b\u60c5\u5883\u9700\u8981\u5728Application\u4e2d\u9032\u884c\u521d\u59cb\u5316\uff0c\u90a3\u5c31\u9700\u8981\u81ea\u5df1\u5efa\u7acb\u4e00\u500b\u5ba2\u88fd\u5316\u7684Application\u7269\u4ef6\uff0c\u5982\u4e0b\u9762Realm\u8cc7\u6599\u5eab\u7684\u521d\u59cb\u5316\u60c5\u5f62\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin line-numbers\">class MyApplication : Application() {\n\n\u00a0   override fun onCreate() {\n\u00a0       super.onCreate()\n\n\u00a0       \/\/ \u521d\u59cb\u5316 Realm \u8cc7\u6599\u5eab\n\u00a0       Realm.init(this)\n\u00a0       val config = RealmConfiguration.Builder()\n\u00a0           .name(\"my.database.db\")\n\u00a0           .build()\n\u00a0       Realm.setDefaultConfiguration(config)\n\u00a0   }\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>\u9664\u4e86\u4e0a\u9762\u4f7f\u7528Realm\u8cc7\u6599\u5eab\uff0c\u9700\u8981\u5728Application\u4e2d\u521d\u59cb\u5316\u7b49\u9019\u985e\u7684\u60c5\u5883\u4e4b\u5916\u3002\u9084\u6709\u500b\u66f4\u5e38\u898b\u7684\u60c5\u5883\u3002<\/p>\n\n\n\n<p>\u6211\u5011\u5f88\u591a\u6642\u5019\u6703\u9700\u8981Contenxt\uff0c\u4f46\u5728Activity\u3001Service\u4e4b\u5916\u662f\u6c92\u6709Context\u53ef\u4ee5\u4f7f\u7528\u7684\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u76f4\u63a5\u5c07Activity\u7684context\u50b3\u905e\u5230\u9700\u8981\u547c\u53eb\u7684\u5730\u65b9\uff0c\u53ef\u80fd\u6703\u56e0\u70ba\u7a0b\u5f0f\u78bc\u8026\u5408\u5ea6\u592a\u9ad8\u3001\u53c8\u6216\u8005\u662fActivity\u5df2\u7d93\u88ab\u91cb\u653e\u4e86\uff0c\u4f46\u547c\u53ebActivity\u7684context\u7684\u7269\u4ef6\u9084\u5b58\u5728\u8457\uff0c\u800c\u9020\u6210\u8a18\u61b6\u9ad4\u6d29\u6f0f\u7b49\u554f\u984c\u767c\u751f\u3002<\/p>\n\n\n\n<p>\u56e0\u6b64\u6211\u5011\u53ef\u4ee5\u900f\u904e\u81ea\u8a02\u4e00\u500bApplication\u7269\u4ef6\uff0c\u8b93\u9700\u8981\u547c\u53ebcontext\u7684\u7269\u4ef6\u76f4\u63a5\u4f7f\u7528Application\u7684context\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin line-numbers\">\/\/ \u81ea\u8a02 Application\nclass MyApplication : Application() {\n\n\u00a0   init {\n\u00a0       instance = this\n\u00a0   }\n\n\u00a0   companion object {\n\u00a0       private lateinit var instance: MyApplication\n\n\u00a0       fun getAppContext(): Context {\n\u00a0           return instance\n\u00a0       }\n\u00a0   }\n}\n\n\/\/ \u4f7f\u7528\u65b9\u5f0f\nval text = MyApplication.getAppContext().getString(R.string.text_msg_hello)<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>\u6700\u5f8c\u88dc\u5145\u8aaa\u660e\uff1a\u7531\u65bcAndroid\u6703\u5728APP\u555f\u52d5\u6642\u5efa\u7acb\u4e00\u500bApplication\u7269\u4ef6\uff0c\u56e0\u6b64\u9019\u500bApplication\u7269\u4ef6\u7684\u751f\u547d\u9031\u671f\u7b49\u65bc\u662f\u6574\u500bAPP\u7684\u751f\u547d\u9031\u671f\u3002<\/p>\n\n\n\n<p>\u4e5f\u56e0\u70baAPP\u5728\u57f7\u884c\u6642\uff0cApplication\u53ea\u6703\u5b58\u5728\u4e00\u500b\u5be6\u4f8b\uff0c\u56e0\u6b64\u624d\u53ef\u4ee5\u88ab\u7576\u6210\u50b3\u905e\u5168\u57df\u8b8a\u6578\u3001\u8b93\u5176\u4ed6\u7269\u4ef6\u4f7f\u7528\u4ed6\u7684context\u7b49\u7528\u9014\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Application\u985e\u5225 \u4ec0\u9ebc\u662fApplication? Application\u548cActivity\u3001Serv &hellip; <\/p>\n","protected":false},"author":1,"featured_media":113,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[32],"class_list":["post-180","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-2","tag-32"],"_links":{"self":[{"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/posts\/180","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=180"}],"version-history":[{"count":2,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/posts\/180\/revisions"}],"predecessor-version":[{"id":1128,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/wp\/v2\/posts\/180\/revisions\/1128"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=\/"}],"wp:attachment":[{"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ray650128.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}