Differences

This shows you the differences between two versions of the page.

Link to this comparison view

smd:laboratoare:06 [2021/04/21 20:15]
adriana.draghici [External storage]
smd:laboratoare:06 [2021/05/20 21:30] (current)
adriana.draghici [Shared preferences]
Line 20: Line 20:
  
 <note important>​Android API Level 29 introduced important changes to how storage is accessed, driven by the need for increased privacy. <note important>​Android API Level 29 introduced important changes to how storage is accessed, driven by the need for increased privacy.
-It provided Scoped Storage([[https://​developer.android.com/​training/​data-storage#​scoped-storage|documentation]][[https://​www.raywenderlich.com/​9577211-scoped-storage-in-android-10-getting-started|tutorial]]). Api Level 30 also introduced some updates related to storage, check [[https://​developer.android.com/​about/​versions/​11/​privacy/​storage|this link]] for some information about them. </​note>​+It provided ​**Scoped Storage** ([[https://​developer.android.com/​training/​data-storage#​scoped-storage|documentation]][[https://​www.raywenderlich.com/​9577211-scoped-storage-in-android-10-getting-started|tutorial]]). Api Level 30 also introduced some updates related to storage, check [[https://​developer.android.com/​about/​versions/​11/​privacy/​storage|this link]] for some information about them. </​note>​
  
 ====Internal Storage==== ====Internal Storage====
Line 93: Line 93:
 sharedPref = this.getSharedPreferences("​mysettings",​ Context.MODE_PRIVATE);​ sharedPref = this.getSharedPreferences("​mysettings",​ Context.MODE_PRIVATE);​
 // or // or
-sharedPref = PreferenceManager.getDefaultSharedPreferences(context);​+sharedPref = PreferenceManager.getDefaultSharedPreferences(context); ​//!!! recently deprecated in API level 29
 </​code>​ </​code>​
  
Line 160: Line 160:
 Provide the functionality from tasks 1 and 2 using internal storage instead of external. Provide the functionality from tasks 1 and 2 using internal storage instead of external.
   * You can choose to store the file using one of the methods described in the [[smd:​laboratoare:​06##​internal_storage|Internal Storage]] section. ​   * You can choose to store the file using one of the methods described in the [[smd:​laboratoare:​06##​internal_storage|Internal Storage]] section. ​
-  * Log the path of the file, try to access it from the command line.+  * Log the path of the file, try to access it from the command line (adb shell).
  
 ==== Task 5 - Shared preferences ==== ==== Task 5 - Shared preferences ====
Line 170: Line 170:
   * When the user selects an item from the spinner, store it as a shared preference. Use [[https://​developer.android.com/​reference/​android/​widget/​AdapterView.html#​setOnItemSelectedListener(android.widget.AdapterView.OnItemSelectedListener)|setOnItemSelectedListener]].   * When the user selects an item from the spinner, store it as a shared preference. Use [[https://​developer.android.com/​reference/​android/​widget/​AdapterView.html#​setOnItemSelectedListener(android.widget.AdapterView.OnItemSelectedListener)|setOnItemSelectedListener]].
   * When the user clicks Download, use an external or internal path, depending on the value stored in the shared preferences. If the value is "​Undefined",​ show a toast telling the user to choose a type of storage.   * When the user clicks Download, use an external or internal path, depending on the value stored in the shared preferences. If the value is "​Undefined",​ show a toast telling the user to choose a type of storage.
 +
 +<spoiler Example >
 +<code java>
 +    private String getStorageType() {
 +        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);​
 +        return sharedPref.getString(getString(R.string.storage_type_key),​ "​undefined"​);​
 +    }
 +
 +    private void storeStorageType(String type)
 +        SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit();​
 +        editor.putString(getString(R.string.storage_type_key),​ type);
 +        editor.apply();​
 +    }
 +
 +    private class StorageTypeSelectedListener implements AdapterView.OnItemSelectedListener {
 +
 +        @Override
 +        public void onItemSelected(AdapterView<?>​ adapterView,​ View view, int i, long l) {
 +            String selectedItem = adapterView.getItemAtPosition(i).toString();​
 +            Log.d(TAG, "​Selected storage type: " +  selectedItem);​
 +            storeStorageType(selectedItem);​
 +        }
 +
 +        @Override
 +        public void onNothingSelected(AdapterView<?>​ adapterView) {
 +        }
 +    }
 +    </​code>​
 +</​spoiler>​
   ​   ​
 Image manipulation code snippets: Image manipulation code snippets:
Line 197: Line 226:
   * [[https://​developer.android.com/​guide/​topics/​data/​data-storage|Android Data Storage documentation]]   * [[https://​developer.android.com/​guide/​topics/​data/​data-storage|Android Data Storage documentation]]
   * [[https://​developer.android.com/​about/​versions/​11/​privacy/​storage|Storage updates in Android 11]]   * [[https://​developer.android.com/​about/​versions/​11/​privacy/​storage|Storage updates in Android 11]]
-  * {{lab6-skel.zip|}}+  * {{lab6_skel.zip|}}
  
smd/laboratoare/06.1619025343.txt.gz ยท Last modified: 2021/04/21 20:15 by adriana.draghici
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0