Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 28

Mobile Application Development

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" />

2 ways to send SMS


• Both ways need manifest permission.
1. Built-in SMS application
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", "default content");
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);
2. SmsManager API
android.telephony.SmsManager
• Manages SMS operations such as sending data, text, and pdu SMS messages. Get this object by
calling the static method getDefault().
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("phoneNo", null, "sms message", null, null);
Networking - SMS
Public Methods
ArrayList<String> divideMessage(String text) Divide a message text into several
fragments, none bigger than the
maximum SMS message size.
static SmsManager getDefault() Get the SmsManager associated
with the default subscription id.
static int getDefaultSmsSubscriptionId() Get default sms subscription id

int getSubscriptionId() Get the associated subscription id.s


void sendDataMessage(String destinationAddress, String scAddr Send a data based SMS to a
ess, short destinationPort, byte[] specific application port.
data, PendingIntent sentIntent, PendingIntent deliveryIntent)
void sendMultimediaMessage(Context context, Uri contentUri, Str Send an MMS message
ing locationUrl, Bundle configOverrides, PendingIntent sentIn
tent)
void sendTextMessage(String destinationAddress, String scAddre Send a text based SMS.
ss, String text, PendingIntent sentIntent, PendingIntent deliv
eryIntent)
DATA
PERSISTENCE
To store data locally on the device and keep your apps working through any
network disruptions for a smooth and consistent user experience.
FILE BASED PERSISTENCE : METHODS
OF LOCAL DATA PERSISTENCE
Android allows to persists application data via the file system. For each application the
Android system creates a data/data/[application package] directory.
Android supports the following ways of storing data in the local file system:
• Files - You can create and update files
• Preferences - Android allows you to save and retrieve persistent key-value pairs of
primitive data type.
• SQLite database - instances of SQLite databases are also stored on the local file
system.
Files are saved in the files folder and application settings are saved as XML files in
the shared_prefs folder.
If your application creates an SQLite database this database is saved in the main
application directory under the databases folder.
INTERNAL VS. EXTERNAL STORAGE
Android has internal storage and external storage.
External storage is not private and may not always be available.

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.

The PreferenceManager class provides methods to get access to these preferences.


The following code shows how to access preferences from a certain file

# getting preferences from a specified file


SharedPreferences settings = getSharedPreferences("Test", Context.MODE_PRIVATE);
Preferences should be created private for the application. They can be accessed via all
application components.
A default store for preferences can be accessed via the PreferenceManager.getDefaultSharedPreferences(this)
method call.
Preference value are accessed via the key and the instance of the SharedPreferences class, as demonstrated in
the following listing.

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getActivity());


String url = settings.getString("url", "n/a");

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.

Editor edit = preferences.edit();


edit.putString("username", "new_value_for_user");
edit.apply();
PREFERENCES: PREFERENCE LISTENER

SharedPreferences prefs =
PreferenceManager.getDefaultSharedPreferences(this);

// Instance field for listener


listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
// Your Implementation
}
};

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:

• Add Google play services to your SDK Packages


• Add Google play services to your project. Here you will need to update: 1) dependencies in
build.gradle; and 2) manifest;

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:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />


<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION" />

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

Criteria criteria = new Criteria();


criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setSpeedRequired(false);
criteria.setCostAllowed(true);

String provider = locationManager.getBestProvider(criteria, true);


FINDING WHERE AM I
Location l = locationManager.getLastKnownLocation(provider);
updateWithNewLocation(l);
locationManager.requestLocationUpdates(provider, 2000, 10,locationListener);
TRACKING YOUR LOCATION
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}

public void onProviderDisabled(String provider) {}


public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status,
Bundle extras) {}
};
TRACKING YOUR LOCATION
onLocationChanged(), which is called when a new location is available.
onProviderDisabled(), when the provider is disabled (e.g., the user turn the GPS off).
onProviderEnabled(), when the provider is enabled (e.g., the user turns the WiFI on).
onStatusChanged(), when the provider status changes. This method is called when a
provider is unable to fetch a location or if the provider has recently become available
after a period of unavailability.

You might also like