Last active
August 29, 2015 14:11
-
-
Save operando/69abed712f501496ceb5 to your computer and use it in GitHub Desktop.
GarumとSharedPreferencesへの異常なまでの愛 ref: http://qiita.com/operandoOS/items/8af20ac09a9d6acb075e
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.os.operando.sharedpreferences.sample; | |
// this -> Context | |
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/data/data/[package name]/shared_prefs/com.os.operando.sharedpreferences.sample_preferences.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AppStatus appStatus = new AppStatus(); | |
if(appStatus.showNotification){ | |
// Notificationを出す処理...とかとか | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version='1.0' encoding='utf-8' standalone='yes' ?> | |
<map> | |
<string name="appName">Garum</string> | |
<int name="startupCount" value="1" /> | |
<boolean name="showNotification" value="true" /> | |
</map> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Pref(name = "user_status") | |
public class UseStatus extends PrefModel { | |
@PrefKey("last_used") | |
public Date lastUsed; | |
@PrefKey | |
public Calendar birthday; | |
@PrefKey("tmp_file") | |
public File tmpFile; | |
@PrefKey("uri") | |
public Uri id_uri; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
UseStatus useStatus = new UseStatus(); | |
useStatus.lastUsed = new Date(); | |
useStatus.birthday = Calendar.getInstance(); | |
useStatus.tmpFile = new File("tmp.txt"); | |
useStatus.id_uri = Uri.parse("content://com.os.operando.sample/users/1"); | |
useStatus.save(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
UseStatus useStatus = new UseStatus(); | |
useStatus.lastUsed = new Date(); | |
useStatus.birthday = Calendar.getInstance(); | |
useStatus.tmpFile = new File("tmp.txt"); | |
useStatus.id_uri = Uri.parse("content://com.os.operando.sample/users/1"); | |
useStatus.save(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.os.operando.sharedpreferences.sample; | |
SharedPreferences sp = getSharedPreferences("app_pref", MODE_PRIVATE); | |
// → /data/data/[package name]/shared_prefs/app_pref_preferences.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SharedPreferences sp = getSharedPreferences("app_pref", MODE_PRIVATE); | |
sp.getBoolean("key_sample", false); | |
sp.edit().putBoolean("key_sample", true).commit(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/data/data/[package name]/shared_prefs/app_pref_preferences.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Pref(name = "app_status") | |
public class AppStatus extends PrefModel { | |
@PrefKey("name") // 別の名前をつける。Keyの名前は「name」となる | |
public String appName; | |
@PrefKey | |
public int startupCount; | |
@PrefKey | |
public boolean showNotification; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Pref(name = "app_status") | |
public class AppStatus extends PrefModel { | |
@PrefKey | |
@DefaultString("test") | |
public String appName; | |
@PrefKey | |
@DefaultInt(redId = R.Integer.test_integer) | |
public int startupCount; | |
@PrefKey | |
@DefaultBoolean(false) | |
public boolean showNotification; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyApplication extends Application { | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
Garum.initialize(getApplicationContext()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyApplication extends Application { | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
Context context = getApplicationContext(); | |
Configuration.Builder builder = new Configuration.Builder(context); | |
builder.setModelClasses(AppStatus.class, PrefModel.class, UseStatus.class); | |
Garum.initialize(builder.create()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AppStatus appStatus = new AppStatus(); | |
appStatus.appName = "Garum"; | |
appStatus.startupCount = ++appStatus.startupCount; | |
appStatus.showNotification = true; | |
appStatus.save(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AppStatus appStatus = new AppStatus(); | |
appStatus.appName = "Garum"; | |
appStatus.startupCount = ++appStatus.startupCount; | |
appStatus.showNotification = true; | |
appStatus.save(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version='1.0' encoding='utf-8' standalone='yes' ?> | |
<map> | |
<string name="appName">Garum</string> | |
<int name="startupCount" value="1" /> | |
<boolean name="showNotification" value="true" /> | |
</map> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment