Files and prefernces

Using the SharedPreferences class you can create named maps of key/value pairs within your
application that can be shared among application components running in the same application
context.

 

 call getSharedPreferences on the application Context

 

 // Retrieve an editor to modify the shared preferences.
SharedPreferences.Editor editor = mySharedPreferences.edit();
// Store new primitive types in the shared preferences object.
editor.putBoolean("isTrue", true);
editor.putFloat("lastFloat", 1f);
editor.putInt("wholeNumber", 2);
editor.putLong("aNumber", 3l);
editor.putString("textEntryValue", "Not Empty");
// Commit the changes.
editor.commit();

 

RETRIEVING SHARED PREFERENCES

 

public static String MY_PREFS = "MY_PREFS";
public void loadPreferences() {
// Get the stored preferences
int mode = Activity.MODE_PRIVATE;
SharedPreferences mySharedPreferences = getSharedPreferences(MY_PREFS, mode);
// Retrieve the saved values.
boolean isTrue = mySharedPreferences.getBoolean("isTrue", false);
float lastFloat = mySharedPreferences.getFloat("lastFloat", 0f);
int wholeNumber = mySharedPreferences.getInt("wholeNumber", 1);
long aNumber = mySharedPreferences.getLong("aNumber", 0);
String stringPreference = mySharedPreferences.getString("textEntryValue", "");
}

 

SharedPreferences prefs;

Context context = getApplicationContext();
prefs = PreferenceManager.getDefaultSharedPreferences(context);

 

Defining a Preference Screen Layout in XML

preference definitions are stored in the res/xml resources folder

 

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
</PreferenceScreen>

 

A simple Shared Preferences screen

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="My Preference Category"/>
<CheckBoxPreference
android:key="PREF_CHECK_BOX"
android:title="Check Box Preference"
android:summary="Check Box Preference Description"
android:defaultValue="true"
/>
</PreferenceCategory>
</PreferenceScreen>

 

Native Preference Controls

CheckBoxPreference

EditTextPreference

ListPreference

RingtonePreference

 

 

 Using Intents to Import System Preference Screens

<PreferenceScreen
android:title="Intent preference"
android:summary="System preference imported using an intent">
<intent android:action="android.settings.DISPLAY_SETTINGS "/>
</PreferenceScreen>

 

Introducing the Preference Activity

public class MyPreferenceActivity extends PreferenceActivity {
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}

 

On Shared Preference Change Listener skeleton implementation

public class MyActivity extends Activity implements
OnSharedPreferenceChangeListener {
@Override
public void onCreate(Bundle SavedInstanceState) {
// Register this OnSharedPreferenceChangeListener
Context context = getApplicationContext();
SharedPreferences prefs =
PreferenceManager.getDefaultSharedPreferences(context);
prefs.registerOnSharedPreferenceChangeListener(this);
}
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
// TODO Check the shared preference and key parameters and change UI or
// behavior as appropriate.
}
}

 

public class UserPreferences extends PreferenceActivity

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}

 

SAVING ACTIVITY STATE

protected void saveActivityPreferences(){
// Create or retrieve the activity preference object.
SharedPreferences activityPreferences = getPreferences(Activity.MODE_PRIVATE)
// Retrieve an editor to modify the shared preferences.
SharedPreferences.Editor editor = activityPreferences.edit();
// Retrieve the View
TextView myTextView = (TextView)findViewById(R.id.myTextView);
// Store new primitive types in the shared preferences object.
editor.putString("currentTextValue", myTextView.getText().toString());
// Commit changes.
editor.commit();
}

 

Saving and Restoring Instance State

private static final String TEXTVIEW_STATE_KEY = "TEXTVIEW_STATE_KEY";
@Override
public void onSaveInstanceState(Bundle saveInstanceState) {
// Retrieve the View
TextView myTextView = (TextView)findViewById(R.id.myTextView);
// Save its state
saveInstanceState.putString(TEXTVIEW_STATE_KEY, myTextView.getText().toString());
super.onSaveInstanceState(saveInstanceState);
}

 

 

SAVING AND LOADING FILES

String FILE_NAME = "tempfile.tmp";
// Create a new output file stream that’s private to this application.
FileOutputStream fos = openFileOutput(FILE_NAME, Context.MODE_PRIVATE);
// Create a new file input stream.
FileInputStream fis = openFileInput(FILE_NAME);

 

String OUTPUT_FILE = "publicCopy.txt";
FileOutputStream fos = openFileOutput(OUTPUT_FILE, Context.MODE_WORLD_WRITEABLE);

 

INCLUDING STATIC FILES AS RESOURCES

Resources myResources = getResources();
InputStream myFile = myResources.openRawResource(R.raw.myfilename);

 

FILE MANAGEMENT TOOLS

deleteFile Enables you to remove files created by the current application.

fileList Returns a string array that includes all the files created by the current application

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值