-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainActivity.java
More file actions
executable file
·47 lines (35 loc) · 1.39 KB
/
MainActivity.java
File metadata and controls
executable file
·47 lines (35 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package net.learn2develop.preferences;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity extends Activity {
SharedPreferences prefs;
String prefName = "MyPref";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//---get the SharedPreferences object---
prefs = getSharedPreferences(prefName, MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
//---save some values using the SharedPreferences object---
editor.putFloat("temperature", 85);
editor.putBoolean("authenticated", true);
editor.putString("username", "Wei-Meng Lee");
//---saves the values---
editor.commit();
readPrefValues();
}
public void readPrefValues() {
prefs = getSharedPreferences(prefName, MODE_PRIVATE);
float temperature = prefs.getFloat("temperature", 50);
boolean authenticated = prefs.getBoolean("authenticated", false);
String username = prefs.getString("username", "");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}