PV239 #android2 cv3 Marek Sedlak @msed__ PERMISSIONS Permissions ● Each app operates in sandbox ● Additional capabilities? -> permissions in AndroidManifest.xml ... ● More levels of protection ○ Normal permissions ■ resources outside the sandbox ■ little risk to the user’s privacy ○ Dangerous permissions ■ contacts Marshmallow changes the flow (API23) ● App requests permission at run-time if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) { if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity, Manifest.permission.READ_CONTACTS)) { // show alert why we need that permisson } else { ActivityCompat.requestPermissions(thisActivity, new String[]{Manifest.permission.READ_CONTACTS}, MY_PERMISSIONS_REQUEST_READ_CONTACTS); // this means we have successfully requested the permission } } STORAGE STORAGE ● SharedPreferences ○ Simple values, String, Integer, Boolean,... ● Internal Storage, External Storage ○ External storage is World R/W ● SQLite DB ○ There are better approaches, like Realm ● Network ○ REST API, Custom server SharedPreferences ● Simple Data ● public static final String PREFS_NAME = "MyPrefsFile"; ● Writing SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); SharedPreferences.Editor editor = settings.edit(); editor.putBoolean("silentMode", mSilentMode); // Commit the edits! editor.commit(); ● Reading SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); boolean silent = settings.getBoolean("silentMode", false); setSilent(silent); Internal Storage ● https://developer.android.com/guide/topics/data/data-storage.html#filesInternal String FILENAME = "hello_file"; String string = "hello world!"; FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE); fos.write(string.getBytes()); fos.close(); External Storage ● https://developer.android.com/guide/topics/data/data-storage.html#filesExterna l ● External storage may be removed, replaced, erased by 3rd party public File getAlbumStorageDir(String albumName) { // Get the directory for the user's public pictures directory. File file = new File(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES), albumName); if (!file.mkdirs()) { Log.e(LOG_TAG, "Directory not created"); } return file; } ● File based database ● There are better alternatives ● Better: Realm (try at home) ○ Has sync options (cross-platform/cloud) SQLite ● JSON ● POST/PUT/GET Http methods ● Retrofit2 Network based storage EXAMPLE See git. THANK YOU See you on Slack.