Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.idea
1 change: 1 addition & 0 deletions AndroidPersistentData/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".settings.SettingsActivity"/>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package expert.android.quoccuong.androidpersistentdata;

import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
Expand All @@ -18,6 +24,9 @@
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import expert.android.quoccuong.androidpersistentdata.settings.SettingsActivity;
import expert.android.quoccuong.androidpersistentdata.settings.UserSettingsChangeListener;

public class MainActivity extends BaseActivity implements View.OnClickListener{

private EditText edtMessage;
Expand All @@ -26,7 +35,10 @@ public class MainActivity extends BaseActivity implements View.OnClickListener{

private static final String FILENAME = "sample.txt";

private SharedPreferences sharedPreferences;

private UserAction recentUserAction;
private UserSettingsChangeListener listener;

enum UserAction {
READ, WRITE
Expand All @@ -46,6 +58,9 @@ protected void onCreate(Bundle savedInstanceState) {
btnRead.setOnClickListener(this);

Log.d("Cuong", "OnCreate");

// sharedPreferences = getSharedPreferences(getPackageName(), MODE_PRIVATE); context
sharedPreferences = getPreferences(MODE_PRIVATE); // activity
}

@Override
Expand All @@ -69,7 +84,8 @@ protected void onResume() {

private void populateTheReadText() {
recentUserAction = UserAction.READ;
try {
txtMessage.setText(sharedPreferences.getString("sample_key", "String not found"));
/*try {
if (arePermissionGranted(EXTERNAL_STORAGE_READ_WRITE_PERMISSION)) {
txtMessage.setText(readTextFromExternalStorage(FILENAME));
txtMessage.setVisibility(View.VISIBLE);
Expand All @@ -80,14 +96,17 @@ private void populateTheReadText() {
} catch (IOException e) {
e.printStackTrace();
txtMessage.setVisibility(View.GONE);
}
}*/
}

private void writeContentToFile() {
recentUserAction = UserAction.WRITE;
String text = edtMessage.getText().toString();
if (!TextUtils.isEmpty(text)) {
try {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("sample_key", text);
editor.commit();
/*try {
if (arePermissionGranted(EXTERNAL_STORAGE_READ_WRITE_PERMISSION)) {
writeToExternalStorageFile(FILENAME, text);
} else {
Expand All @@ -96,7 +115,7 @@ private void writeContentToFile() {
}
} catch (IOException e) {
e.printStackTrace();
}
}*/
}
}

Expand All @@ -111,4 +130,32 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
}
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu_main_activity, menu);
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

listener = new UserSettingsChangeListener(getApplicationContext());
switch (item.getItemId()) {
case R.id.menu_settings:
startActivity(new Intent(getApplicationContext(), SettingsActivity.class));
PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).registerOnSharedPreferenceChangeListener(listener);
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}

@Override
protected void onDestroy() {
super.onDestroy();
PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).unregisterOnSharedPreferenceChangeListener(listener);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package expert.android.quoccuong.androidpersistentdata.settings;

import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.support.annotation.Nullable;

import expert.android.quoccuong.androidpersistentdata.R;

/**
* Created by CuongDuong on 1/17/2018.
*/

public class SettingsActivity extends PreferenceActivity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.user_pref_settings);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package expert.android.quoccuong.androidpersistentdata.settings;

import android.content.Context;
import android.content.SharedPreferences;
import android.widget.Toast;

/**
* Created by CuongDuong on 1/17/2018.
*/

public class UserSettingsChangeListener implements SharedPreferences.OnSharedPreferenceChangeListener {

private Context mContext;

public UserSettingsChangeListener(Context mContext) {
this.mContext = mContext;
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
Toast.makeText(mContext, s + " value changed: " + sharedPreferences.getString(s, "Unknow"), Toast.LENGTH_SHORT).show();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="Hello World!"
android:visibility="gone" />
android:visibility="visible" />

</RelativeLayout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_settings" android:title="Settings" />
</menu>
25 changes: 25 additions & 0 deletions AndroidPersistentData/app/src/main/res/xml/user_pref_settings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

<EditTextPreference
android:defaultValue="Default value"
android:key="display_name_key"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="Display Name" />
<PreferenceCategory android:title="App Settings">

<CheckBoxPreference
android:defaultValue="false"
android:key="enable_otp_read"
android:title="Enable Auto Read OTP" />
<RingtonePreference
android:defaultValue=""
android:key="ringtone_preference_1"
android:title="Ringtone preference" />
<SwitchPreference
android:defaultValue="false"
android:key="receive_notification"
android:title="Enable Notifications" />
</PreferenceCategory>
</PreferenceScreen>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions note.txt
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,26 @@ And we may want to share it with another app in that case you have to use Custom

4. Path of file: data/data/<package_name> in Device File Explorer.

----------------------------
SHARED PREFERENCES

1. Store non sensitive data (language, fontsize, audio volume, cache username...) in xml.

2. <Key, Value> and no parsing

3. Data types: boolean, float, int, long, String, Set<String>

4. get:
_ Context.getSharedPreferences
_ Activity.getPreferences

5. put and get

_ PUT:
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("sample_key", text);
editor.commit();

_ GET:
sharedPreferences.getString("sample_key", "String not found")