Professional Documents
Culture Documents
9. Content Provider, Data Persistence
9. Content Provider, Data Persistence
CONTENT
PROVIDER
Content providers can help an application manage
access to data stored by itself or stored by other apps
and provide a way to share data with other apps. They
encapsulate the data and provide mechanisms for
defining data security. Content providers are the
standard interface that connects data in one process
with code running in another process.
ADVANTAGES OF CONTENT
PROVIDERS
Content providers offer granular control over the permissions for accessing data. You can
choose to restrict access to only a content provider that is within your application, grant blanket
permission to access data from other applications, or configure different permissions for reading
and writing data. For more information about using content providers securely, see the security
tips for data storage and Content provider permissions.
You can use a content provider to abstract away the details for accessing different data sources
in your application. For example, your application might store structured records in a SQLite
database, as well as video and audio files. You can use a content provider to access all of this
data.
Also, CursorLoader objects rely on content providers to run asynchronous queries and then
return the results to the UI layer in your application. For more information about using a
CursorLoader to load data in the background, see Loaders.
A content provider coordinates
access to the data storage layer
in your application for a number of
different APIs and components
INTERACTION BETWEEN CONTENT
PROVIDER, OTHER CLASSES, AND
STORAGE
Networking - SMS
Manifest permission
<uses-permission android:name="android.permission.SEND_SMS" />
If, for example, the Android device is connected with a computer, the computer may mount the
external system via USB and that makes this external storage not available for Android
applications.
As of Android 8 SDK level it is possible to define that the application can or should be placed
on external storage. For this set the android:installLocation to preferExternal or auto.
In this case certain application components may be stored on an encrypted external mount
point. Database and other private data will still be stored in the internal storage system.
PREFERENCES: STORING KEY-VALUE PAIRS
The SharedPreferences class allows to persists key-value pairs of primitive data types in
the Android file system.
To create or change preferences you have to call the edit() method on the SharedPreferences object. Once you
have changed the value you have to call the apply() method to apply your asynchronously to the file system.
SharedPreferences prefs =
PreferenceManager.getDefaultSharedPreferences(this);
prefs.registerOnSharedPreferenceChangeListener(listener);
LOCATION
BASED
SERVICES
(MAPS)
What makes mobile so different is we can build advanced services that only mobile
device with sensing can deliver. We do this all the time on our phone: do a location
based search for say pizza store, cafe, cinema -- and the phone uses the location of the
phone as an input to the search. This is called location based service.
SETTING UP GOOGLE PLAY SERVICES
To use Location (needed for this lecture), Google Maps, Activity Recognition, Cloud services have to
set up your environment to use Google Play Services -- if you don't then you can't do anything. You
have to:
Below we shows a list of the separate APIs that you can include when compiling your app; specifically
we add dependencies for location/ activity recognition and maps.
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.0'
implementation 'com.google.android.gms:play-services-location:11.8.0'
implementation 'com.google.android.gms:play-services-maps:11.8.0'
testImplementation 'junit:junit:4.12'
}
You should also make sure for this lecture that you update your manifest with
permission associated with the level of location updates; as shown below:
and to specify that the project is using the play services and its version
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
LOCATION MANAGER
The Android location manager gives location in terms of longitude and latitude for the location of
the phone. Depending on the location provider selected (could be based on GPS, WiFi or
Cellular) the accuracy of the location will vary.
The key Android plumbing for location is:
• Location Manager, which provides the coordinates
• Location Providers, which can make a number of trade offs to offer the user the capability
they want
A number of services can be built using these simple components:
• get the user's current location
• periodically get the user location as the move around -- provides a trail of bread crumbs
• use proximity alerts when you move in and out of a predefined area (e.g., Time Square)
public class WhereAmI extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager locationManager;
String svcName = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(svcName);
In the Manifest you will see that it is necessary to get the user's permission to track their location or
get a location reading:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
LOCATION PROVIDER
The user can specify the location provider explicitly in the code using a number of
constants:
LocationManager.GPS_PROVIDER
LocationManager.NETWORK_PROVIDER
LocationManager.PASSIVE_PROVIDER
LOCATION PROVIDER