{"id":12982,"date":"2025-12-12T22:44:36","date_gmt":"2025-12-12T13:44:36","guid":{"rendered":"https:\/\/pineplanter.moo.jp\/non-it-salaryman\/?p=12982"},"modified":"2025-12-12T22:44:36","modified_gmt":"2025-12-12T13:44:36","slug":"android-sharedpreferences","status":"publish","type":"post","link":"https:\/\/pineplanter.moo.jp\/non-it-salaryman\/2025\/12\/12\/android-sharedpreferences\/","title":{"rendered":"\u3010Android\u958b\u767a\u3011\u30ed\u30fc\u30ab\u30eb\u30b9\u30c8\u30ec\u30fc\u30b8(SharedPreferences)\u306e\u4f7f\u3044\u65b9\u30e1\u30e2"},"content":{"rendered":"<p>Android\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u958b\u767a\u306b\u304a\u3044\u3066\u3001\u30e6\u30fc\u30b6\u30fc\u306e\u8a2d\u5b9a\u3084\u5c11\u91cf\u306e\u30c7\u30fc\u30bf\u3092\u6c38\u7d9a\u7684\u306b\u4fdd\u5b58\u3059\u308b\u3053\u3068\u306f\u4e0d\u53ef\u6b20\u3067\u3059\u3002SharedPreferences\u306e\u4e3b\u306a\u76ee\u7684\u306f\u3001\u5c11\u91cf\u306e\u30c7\u30fc\u30bf\u3092\u52b9\u7387\u7684\u306b\u4fdd\u5b58\u3057\u4e14\u3064\u30a2\u30d7\u30ea\u304c\u7d42\u4e86\u3057\u305f\u308a\u518d\u8d77\u52d5\u3057\u305f\u308a\u3057\u305f\u5f8c\u3082\u7dad\u6301\u3055\u308c\u308b\u3082\u306e\u3067\u3059\u3002\u8907\u96d1\u306a\u69cb\u9020\u5316\u30c7\u30fc\u30bf\u3084\u5927\u91cf\u306e\u30c7\u30fc\u30bf\u3092\u6271\u3046\u306e\u306b\u306f\u9069\u3057\u307e\u305b\u3093\u304c\u3001\u30a2\u30d7\u30ea\u306e\u52d5\u4f5c\u3092\u5236\u5fa1\u3059\u308b\u305f\u3081\u306e\u30b7\u30f3\u30d7\u30eb\u306a\u8a2d\u5b9a\u5024\u3092\u7ba1\u7406\u3059\u308b\u4e0a\u3067\u52b9\u7387\u7684\u306a\u4ed5\u7d44\u307f\u3067\u3059\u3002<\/p>\n<h5>SharedPreferences\u3067\u4f7f\u3046import\u6587<\/h5>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport android.content.Context;\r\nimport android.content.SharedPreferences;\r\nimport java.util.Map;\r\nimport java.util.HashMap;\r\n \r\n<\/pre>\n<p>&nbsp;<\/p>\n<h5>SharedPreferences\u3078\u306e\u4fdd\u5b58<\/h5>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\/\/ \u73fe\u5728\u306e\u65e5\u6642\u3092\u30d5\u30a9\u30fc\u30de\u30c3\u30c8\u3057\u3066\u30e6\u30cb\u30fc\u30af\u306a\u30ad\u30fc\u3092\u751f\u6210\r\n\/\/ \u4f8b: &#039;2025-11-29 09:35:12&#039; \u306e\u3088\u3046\u306a\u6587\u5b57\u5217\r\nSimpleDateFormat sdf = new SimpleDateFormat(&quot;yyyyMMdd_HHmm&quot;, Locale.JAPAN);\r\nString uniqueKey = sdf.format(new Date());\r\n\r\nString text= &quot;\u4fdd\u5b58\u3059\u308b\u30c6\u30ad\u30b9\u30c8&quot;;\r\n\r\n\/\/  SharedPreferences \u306e\u53d6\u5f97\uff08\u30a2\u30d7\u30ea\u5c02\u7528\u306e\u4fdd\u5b58\u9818\u57df\uff09\r\nSharedPreferences prefs = getSharedPreferences(&quot;MyAppData&quot;, Context.MODE_PRIVATE);\r\nSharedPreferences.Editor editor = prefs.edit();\r\n\r\n\/\/ \u65e5\u6642\u30d9\u30fc\u30b9\u306e\u30e6\u30cb\u30fc\u30af\u30ad\u30fc\u3092\u4f7f\u3063\u3066\u6587\u5b57\u5217\u3092\u4fdd\u5b58\r\neditor.putString(uniqueKey, text);\r\neditor.apply(); \/\/ \u975e\u540c\u671f\u3067\u4fdd\u5b58\u3092\u5b9f\u884c\r\n \r\n<\/pre>\n<p>&nbsp;<\/p>\n<h5>SharedPreferences\u306b\u4fdd\u5b58\u3055\u308c\u3066\u3044\u308b\u3059\u3079\u3066\u306e\u30c7\u30fc\u30bf\u306e\u53d6\u5f97<\/h5>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\/**\r\n* * @return \u4fdd\u5b58\u3055\u308c\u3066\u3044\u308b\u30ad\u30fc\u3068\u5024\u306e\u30da\u30a2\u306e\u30de\u30c3\u30d7\u3002\r\n* \u30ad\u30fc\u306f\u4fdd\u5b58\u6642\u306e\u30bf\u30a4\u30e0\u30b9\u30bf\u30f3\u30d7\r\n*\/\r\nprivate Map&lt;String, String&gt; loadAllData() {\r\n  \/\/ 1. \u4fdd\u5b58\u6642\u3068\u540c\u3058\u540d\u524d (&quot;MyAppData&quot;) \u3067SharedPreferences\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u53d6\u5f97\r\n  SharedPreferences prefs = getSharedPreferences(&quot;MyAppData&quot;, Context.MODE_PRIVATE);\r\n\r\n  \/\/ 2. \u3059\u3079\u3066\u306e\u30ad\u30fc\u3068\u5024\u306e\u30da\u30a2\u3092\u53d6\u5f97\r\n  \/\/ getAll()\u306f &lt;String, ?&gt; \u306eMap\u3092\u8fd4\u3059\u305f\u3081\u3001\u578b\u5b89\u5168\u306e\u305f\u3081\u306b\u30ad\u30e3\u30b9\u30c8\u3002\r\n  Map&lt;String, ?&gt; allEntries = prefs.getAll();\r\n\r\n  \/\/ 3. \u53d6\u5f97\u3057\u305fMap\u3092 &lt;String, String&gt; \u306b\u7d5e\u308a\u8fbc\u307f\u3001\u65b0\u3057\u3044Map\u3092\u4f5c\u6210\r\n  Map&lt;String, String&gt; dataMap = new HashMap&lt;&gt;();\r\n  for (Map.Entry&lt;String, ?&gt; entry : allEntries.entrySet()) {\r\n    \/\/ SharedPreferences\u306b\u4fdd\u5b58\u3057\u305f\u306e\u306fString\u578b\u306a\u306e\u3067\u3001String\u3068\u3057\u3066\u51e6\u7406\r\n    if (entry.getValue() instanceof String) {\r\n      dataMap.put(entry.getKey(), (String) entry.getValue());\r\n    }\r\n  }\r\n\r\n  return dataMap;\r\n}\r\n \r\n<\/pre>\n<p>&nbsp;<\/p>\n<h5>SharedPreferences\u306b\u4fdd\u5b58\u3055\u308c\u3066\u3044\u308b\u30c7\u30fc\u30bf\u306e\u5168\u6d88\u53bb<\/h5>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nprivate void clearAllData() {\r\n  \/\/ \u4fdd\u5b58\u6642\u3068\u540c\u3058\u540d\u524d (&quot;MyAppData&quot;) \u3067SharedPreferences\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u53d6\u5f97\r\n  SharedPreferences prefs = getSharedPreferences(&quot;MyAppData&quot;, Context.MODE_PRIVATE);\r\n  \/\/ Editor\u3092\u53d6\u5f97\r\n  SharedPreferences.Editor editor = prefs.edit();\r\n  \/\/ \u3059\u3079\u3066\u306e\u30c7\u30fc\u30bf\u3092\u30af\u30ea\u30a2\r\n  editor.clear();\r\n  \/\/ \u975e\u540c\u671f\u3067\u524a\u9664\u3092\u5b9f\u884c\r\n  editor.apply();\r\n}\r\n \r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Android\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u958b\u767a\u306b\u304a\u3044\u3066\u3001\u30e6\u30fc\u30b6\u30fc\u306e\u8a2d\u5b9a\u3084\u5c11\u91cf\u306e\u30c7\u30fc\u30bf\u3092\u6c38\u7d9a\u7684 &#8230; <\/p>\n","protected":false},"author":1,"featured_media":12987,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-12982","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-json\/wp\/v2\/posts\/12982","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-json\/wp\/v2\/comments?post=12982"}],"version-history":[{"count":5,"href":"https:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-json\/wp\/v2\/posts\/12982\/revisions"}],"predecessor-version":[{"id":12988,"href":"https:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-json\/wp\/v2\/posts\/12982\/revisions\/12988"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-json\/wp\/v2\/media\/12987"}],"wp:attachment":[{"href":"https:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-json\/wp\/v2\/media?parent=12982"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-json\/wp\/v2\/categories?post=12982"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pineplanter.moo.jp\/non-it-salaryman\/wp-json\/wp\/v2\/tags?post=12982"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}