Ricta Scott
Ricta Scott

Reputation: 65

SAVE button to store data and retrieve later

I have looked around for the answer to this question, and I'm sure I have read it, but I dont understand it. Hope someone can help.

I want to save 'Name' 'Address' 'Support' (text, text, number respectively) to a file and then retrieve the info later for use (days, months, after phone reboot) in sending a text msg.

I found openFileOutput() but I cant work out how to use it.

I am new to coding so don't really understand where the examples I have found belong. Any help much appreciated.

EDIT---- What I am trying to do is: Create an app that will be a simple one button app for the end user. Install it, add NAME, ADDRESS, and SUPPORT (mobile/cell number) to the "Settings" page. Later, when you want help, the end user starts the app and hits the button. The app retrieves the NAME, ADDRESS and sends them in a text to the SUPPORT number Any more help very much appreciated.

Upvotes: 0

Views: 1042

Answers (2)

sdabet
sdabet

Reputation: 18670

If you want to go with preferences, here is how you can do it.

1) Declare the preferences you want to store in an XML file, e.g res/xml/preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <EditTextPreference
        android:key="pref_name"
        android:title="Name"
        android:defaultValue="" />
    <EditTextPreference
        android:key="pref_address"
        android:title="Address"
        android:defaultValue="" />
    <EditTextPreference
        android:key="pref_support"
        android:title="Support"
        android:defaultValue="" />
</PreferenceScreen>

2) Extend PreferenceActivity to display a screen that will allow modification of these preferences:

public class TestActivity extends PreferenceActivity {

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

3) Use these preferences to send an email for instance:

private void sendEmail(String emailAddress) {
    String name = getPreferences(0).getString("pref_name", "");
    String address = getPreferences(0).getString("pref_address", "");
    String support = getPreferences(0).getString("pref_support", "");

    String mailBody = "Name: " + name + "\nAddress: " + address + "\nSupport: " + support;

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/html");
    intent.putExtra(Intent.EXTRA_EMAIL, emailAddress);
    intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
    intent.putExtra(Intent.EXTRA_TEXT, mailBody);

    startActivity(Intent.createChooser(intent, "Send Email"));  
}

Upvotes: 1

Ram kiran Pachigolla
Ram kiran Pachigolla

Reputation: 21191

If the values remain small, and you don't need them to be structured (like if you have user profiles or something), then Shared Preferences should be just fine. 100 ints only amounts to 400 bytes, so even if the Shared Preferences were stored in memory, it's not a big deal.

click here for complete information about shared preferences

Upvotes: 1

Related Questions