Android Unit 3
Android Unit 3
Android Unit 3
Storing Data
Lesson 9
● Save new files that the user acquires through your app to
a public directory where other apps can access them and
the user can easily copy them from the device
● Save external files in public directories
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE" />
This work is licensed under a Creative
Android Developer Fundamentals Storing Data Commons Attribution-NonCommercial 16
4.0 International License
Always check availability of storage
developer.android.com/reference/android/os/Environment.html
This work is licensed under a Creative
Android Developer Fundamentals Storing Data Commons Attribution-NonCommercial 18
4.0 International License
Accessing public external directories
1. Get a path getExternalStoragePublicDirectory()
2. Create file
● External storage
myFile.delete();
● Internal storage
myContext.deleteFile(fileName);
● SharedPreferences.Editor interface
● Takes care of all file operations
● put methods overwrite if key exists
● apply() saves asynchronously and safely
SharedPreferences.Editor preferencesEditor =
mPreferences.edit();
preferencesEditor.clear();
preferencesEditor.apply();
SharedPreferences.OnSharedPreferenceChangeListener listener =
new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(
SharedPreferences prefs, String key) {
// Implement listener here
}
};
prefs.registerOnSharedPreferenceChangeListener(listener);
// Column names...
public static final String KEY_ID = "_id";
public static final String KEY_WORD = "word";
@Override
public void onCreate(SQLiteDatabase db) { // Creates new database
// Create the tables
db.execSQL(WORD_LIST_TABLE_CREATE);
// Add initial data
...
}
● SQLiteDatabase.rawQuery()
Use when data is under your control and supplied only
by your app
● SQLiteDatabase.query()
Use for all other queries
This work is licensed under a Creative
Android Developer Fundamentals Storing Data Commons Attribution-NonCommercial 68
4.0 International License
SQLiteDatabase.rawQuery() format
newId = mWritableDB.insert(
WORD_LIST_TABLE,
null,
values);
deleted = mWritableDB.delete(
WORD_LIST_TABLE,
KEY_ID + " =? ",
new String[]{String.valueOf(id)});
mNumberOfRowsUpdated = mWritableDB.update(
WORD_LIST_TABLE,
values, // new values to insert
KEY_ID + " = ?",
new String[]{String.valueOf(id)});
● In MainActivity onCreate()
● What is a ContentProvider
● App with Content Provider Architecture
● Implementation
○ Contract
○ ContentProvider
○ Manifest Permissions
○ Content Resolver
Contract specifies
● URIs to query data
● Table structure of data
● MIME type of returned data
● Constants
This work is licensed under a Creative
Android Developer Fundamentals Storing Data Commons Attribution-NonCommercial 93
4.0 International License
Content Provider
<manifest ...>
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
This work is licensed under a Creative
Android Developer Fundamentals Storing Data Commons Attribution-NonCommercial 99
4.0 International License
Example permissions
ACCESS_COARSE_LOCATION RECEIVE_SMS
ACCESS_FINE_LOCATION CAMERA
ACCESS_NETWORK_STATE RECORD_AUDIO
ACCESS_WIFI_STATE MODIFY_AUDIO_SETTINGS
GET_ACCOUNTS ADD_VOICEMAIL
See more at
https://developer.android.com/reference/android/Manifest.permission.html
Marshmallow onwards
● Installation doesn't ask user to give
permissions
● App must get runtime permission
● Marshmallow onwards
Revoke individual permissions
Settings > apps > permissions
● Permissions in Android:
developer.android.com/guide/topics/security/permissions.html
● Best practices:
developer.android.com/training/permissions/best-practices.html
● Speed
● Responsiveness
● Smoothness
● Consistency
● Resource-efficiency
● Be systematic
○ Gather information
○ Gain insight
○ Take action
● Iterate
● Use tools
This work is licensed under a Creative
Android Developer Fundamentals Storing Data Commons Attribution-NonCommercial 114
4.0 International License
Profile GPU Rendering tool
This
Thiswork
workisislicensed
licensedunder
undera aCreative
Creative
Android Developer Fundamentals Storing Data Commons
CommonsAttribution-NonCommercial
Attribution-NonCommercial 120
4.0
4.0International
InternationalLicense
License
Android's got you covered (mostly)
● Saving files:
developer.android.com/training/basics/data-storage/files.html
● Sharing files:
developer.android.com/training/secure-file-sharing/index.html
This
Thiswork
workisislicensed
licensedunder
undera aCreative
Creative
Android Developer Fundamentals Storing Data Commons
CommonsAttribution-NonCommercial
Attribution-NonCommercial 132
4.0
4.0International
InternationalLicense
License
What is Firebase?
Firebase Console
is a web front end
for managing your
Firebase projects
● App-level build.gradle…
// Add to the bottom of the file
apply plugin: 'com.google.gms.google-services'
firebase.google.com/docs/analytics/
This work is licensed under a Creative
Android Developer Fundamentals Storing Data Commons Attribution-NonCommercial 146
4.0 International License
Firebase Analytics: default reports
Get reports without
adding code
● geographic
● demographic
● engagement
● revenue
This work is licensed under a Creative
Android Developer Fundamentals Storing Data Commons Attribution-NonCommercial 147
4.0 International License
Firebase Analytics: custom reports
firebase.google.com/docs/test-lab/
● Firebase
● Firebase Testing Lab
● Getting started with Firebase for Android
● Firebase in a weekend online course
www.udacity.com/course/ud0352
codelabs.developers.google.com/codelabs/firebase-android
developer.android.com/google/play/billing/index.html
This work is licensed under a Creative
Android Developer Fundamentals Storing Data Commons Attribution-NonCommercial 174
4.0 International License
In-app purchases
Use In-app purchases to sell extra features
● New features
● Additional content
● Skins
● New levels, powers, attacks...
developer.android.com/distribute/monetize/freemium.html
developer.android.com/distribute/monetize/ads.html
This work is licensed under a Creative
Android Developer Fundamentals Storing Data Commons Attribution-NonCommercial 177
4.0 International License
Sign up for AdMob in Firebase Console
● Earn
● Monetizing with Ads
● In-App Purchases
● In-App Billing
● Firebase codelab
codelabs.developers.google.com/codelabs/firebase-android
This
Thiswork
workisislicensed
licensedunder
undera aCreative
Creative
Android Developer Fundamentals Storing Data Commons
CommonsAttribution-NonCommercial
Attribution-NonCommercial 181
4.0
4.0International
InternationalLicense
License
Contents
On the device
● Home screen
● Manage Applications
● My Downloads This work is licensed under a Creative
Android Developer Fundamentals Storing Data Commons Attribution-NonCommercial 192
4.0 International License
Google Play filters search results
developer.android.com/google/play/filters.html
tools.android.com/tech-docs/new-build-system/applicationid-vs-packagename
developer.android.com/topic/performance/reduce-apk-size.html
developer.android.com/topic/performance/reduce-apk-size.html
developer.android.com/studio/projects/index.html#ApplicationProjects
● src: source files for your app (.java and .aidl files)
NO jar files
● lib: third-party or private library files, including prebuilt
shared and static libraries (such as .so files)
● jni: source files associated with the Android NDK, such as
.c, .cpp, .h, and .mk files
This
Thiswork
workisislicensed
licensedunder
undera aCreative
Creative
Android Developer Fundamentals Storing Data Commons
CommonsAttribution-NonCommercial
Attribution-NonCommercial 213
4.0
4.0International
InternationalLicense
License
Publish your app!
Publish your Android apps on
Google Play
Users can
● search
● download
● review
1. Go to play.google.com/apps/publish/
2. Accept agreement
3. Pay the registration fee
4. Enter your details
play.google.com/apps/publish/
This work is licensed under a Creative
Android Developer Fundamentals Storing Data Commons Attribution-NonCommercial 219
4.0 International License
Enter your details
● Name
● Address
● Website
● Phone
● Email
preferences
This work is licensed under a Creative
Android Developer Fundamentals Storing Data Commons Attribution-NonCommercial 220
4.0 International License
You're in!
● Test your app before public release to help find and fix
any technical or user experience issues
● Get the bugs out before you release your app to the world!
● Feedback from your test users does not affect your app’s
public rating
● For open tests (alpha or beta) program, you can see and
reply to user feedback in the Developer Console
● Alpha and beta feedback from users is only visible to you
and cannot be seen in the Google Play store
play.google.com/about/developer-content-policy/
This work is licensed under a Creative
Android Developer Fundamentals Storing Data Commons Attribution-NonCommercial 241
4.0 International License
Restricted Content
● Sexually Explicit Content
● Child Endangerment
● Violence
● Bullying & Harassment
● Hate Speech
● Sensitive Events
● Gambling
● Illegal Activities
play.google.com/about/restricted-content
This work is licensed under a Creative
Android Developer Fundamentals Storing Data Commons Attribution-NonCommercial 242
4.0 International License
Publish to the
world!
● Dev guide:
developer.android.com/distribute/googleplay/developer-console.html
● Help center:
support.google.com/googleplay/android-developer/#topic=3450769