mobile application, assignment help

User Generated

onyn

Computer Science

Description

Application

Follow the steps in Activity12.1 attached below. Get screenshots and explain the different parts of your app.

Questions

Answer and explain the following questions:

1. Why are application preferences described as sets of data values that are stored persistently?

a. The application data cannot be erased.

b. The application data can only be erased in certain lifecycle events.

c. The application data can be lost if you close the application.

d. The application can be started and stopped without losing the data.

2. Which of the following is not a data type that is supported as preference-setting values?

a. Float values

b. Fixed values

c. Long values

d. Boolean values

3. Which one of the following is a step you take in order to add preferences support to your application?

a. Make changes to the preferences using the Editor.

b. Use the android:Preferences attribute.

c. Locate the preferences in the android.support package.

d. Use the onSupport() callback method.

4. What does the SharedPreferences.Editor.clear() method do?

a. Removes all preferences

b. Clears the application data

c. Removes a specific preference

d. Commits all preference changes

5. Application preferences are stored internally as what type of file?

a. DOCX

b. XHTML

c. XML

d. TXT

6. What class do you use if you have a group of user settings and you want to create a simple way in which the user can edit them?

a. The PreferenceEdit class

b. The PreferenceActivity class

c. The PreferenceAccess class

d. The PreferenceIntent class

7. Which preference was released in Android API Level 11 and is not backward compatible?

a. ListPreference

b. ViewPreference

c. CancelPreference

d. MultiSelectListPreference

8. Which attribute is used to give more details about a preference?

a. android:detail

b. android:synopsis

c. android:summary

d. android:statement

9. Which feature, introduced in Android 3.0, allows your application to present a list of options for navigating to setting subscreens?

a. The titles feature

b. The headers feature

c. The list feature

d. The directory feature

10. Which feature was introduced on Android Marshmallow that easily implements a full data backup and restore?

a. Google Play Store

b. iCloud

c. Cloud Save

d. Auto Backup for Apps

Unformatted Attachment Preview

Activity 12.1 Android Shared Preferences In this activity, we’ll use Shared Preferences in our android application to store data in the form of key-value pair. Android Shared Preferences Overview Shared Preferences allows activities and applications to keep preferences, in the form of keyvalue pairs similar to a Map that will persist even when the user closes the application. Android stores Shared Preferences settings as XML file in shared_prefs folder under DATA/data/{application package} directory. The DATA folder can be obtained by calling Environment.getDataDirectory(). SharedPreferences is application specific, i.e. the data is lost on performing one of the following options:   on uninstalling the application on clearing the application data (through Settings) As the name suggests, the primary purpose is to store user-specified configuration details, such as user specific settings, keeping the user logged into the application. To get access to the preferences, we have three APIs to choose from: getPreferences() : used from within your Activity, to access activity-specific preferences   getSharedPreferences() : used from within your Activity (or other application Context), to access application-level preferences getDefaultSharedPreferences() : used on the PreferenceManager, to get the shared preferences that work in concert with Android’s overall preference framework In this tutorial we’ll go with getSharedPreferences(). The method is defined as follows: getSharedPreferences (String PREFS_NAME, int mode) PREFS_NAME is the name of the file. mode is the operating mode. Following are the operating modes applicable:       MODE_PRIVATE: the default mode, where the created file can only be accessed by the calling application MODE_WORLD_READABLE: Creating world-readable files is very dangerous, and likely to cause security holes in applications MODE_WORLD_WRITEABLE: Creating world-writable files is very dangerous, and likely to cause security holes in applications MODE_MULTI_PROCESS: This method will check for modification of preferences even if the Shared Preference instance has already been loaded MODE_APPEND: This will append the new preferences with the already existing preferences MODE_ENABLE_WRITE_AHEAD_LOGGING: Database open flag. When it is set, it would enable write ahead logging by default Initialization We need an editor to edit and save the changes in shared preferences. The following code can be used to get the shared preferences. SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode Editor editor = pref.edit(); Storing Data editor.commit() is used in order to save changes to shared preferences. editor.putBoolean("key_name", true); // Storing boolean - true/false editor.putString("key_name", "string value"); // Storing string editor.putInt("key_name", "int value"); // Storing integer editor.putFloat("key_name", "float value"); // Storing float editor.putLong("key_name", "long value"); // Storing long editor.commit(); // commit changes Retrieving Data Data can be retrieved from saved preferences by calling getString() as follows: pref.getString("key_name", null); // getting String pref.getInt("key_name", null); // getting Integer pref.getFloat("key_name", null); // getting Float pref.getLong("key_name", null); // getting Long pref.getBoolean("key_name", null); // getting boolean Clearing or Deleting Data remove(“key_name”) is used to delete that particular value. clear() is used to remove all data. editor.remove("name"); // will delete key name editor.remove("email"); // will delete key email editor.commit(); // commit changes editor.clear(); editor.commit(); // commit changes Android Shared Preferences Project Code The activity_main.xml layout consists of two EditText views which store and display name and email. The three buttons implement their respective onClicks in the MainActivity. activity_main.xml The MainActivity.java file is used to save and retrieve the data through keys. MainActivity.java package com.example.sharedpreferences; import import import import import import import android.app.Activity; android.content.Context; android.content.SharedPreferences; android.os.Bundle; android.view.Menu; android.view.View; android.widget.TextView; public class MainActivity extends Activity { SharedPreferences sharedpreferences; TextView name; TextView email; public static final String mypreference = "mypref"; public static final String Name = "nameKey"; public static final String Email = "emailKey"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); name = (TextView) findViewById(R.id.etName); email = (TextView) findViewById(R.id.etEmail); sharedpreferences = getSharedPreferences(mypreference, Context.MODE_PRIVATE); if (sharedpreferences.contains(Name)) { name.setText(sharedpreferences.getString(Name, "")); } if (sharedpreferences.contains(Email)) { email.setText(sharedpreferences.getString(Email, "")); } } public void Save(View view) { String n = name.getText().toString(); String e = email.getText().toString(); SharedPreferences.Editor editor = sharedpreferences.edit(); editor.putString(Name, n); editor.putString(Email, e); editor.commit(); } public void clear(View view) { name = (TextView) findViewById(R.id.etName); email = (TextView) findViewById(R.id.etEmail); name.setText(""); email.setText(""); } public void Get(View view) { name = (TextView) findViewById(R.id.etName); email = (TextView) findViewById(R.id.etEmail); sharedpreferences = getSharedPreferences(mypreference, Context.MODE_PRIVATE); if (sharedpreferences.contains(Name)) { name.setText(sharedpreferences.getString(Name, "")); } if (sharedpreferences.contains(Email)) { email.setText(sharedpreferences.getString(Email, "")); } } } mypreference is the name of the file where the shared preferences key-value pair is stored. The image below shows the final output of our project:
Purchase answer to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Explanation & Answer

Hello, Am ...


Anonymous
Just what I needed…Fantastic!

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4

Similar Content

Related Tags