You are on page 1of 26

Lecture 7


Location Based Services
•Services that, with the help of mobile networks, can provide geographical position information about mobile devices like phone. •What you can do with this ? –Determine your location and directions –Determine the nearest hospital, restaurant, … –Locate friends and family members –And more


• GPS is the most common location provider in Android Solutions •Cell tower triangulation is another method to do the job.

•It provides more accuracy

Android Location Classes
•Location •LocationManager


Location Class
•Represent Geographical Location •Location represented by latitude and longitude

Location Manager
•Provide access to location services •Services allow applications to obtain updates of the devices geographical location.

Location Manager
LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); locManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, MINIMUM_TIME_TO_UPDATE, MINIMUM_DISTANCE_TO_UPDATE , new MyLocationListener());


Get Cached Location

Location Providers

Better Scenario
•Get Cached Location –locManager.

•Use Network Provider To get new location fast •Use GPS Provider to get more accurate location

•getBestProvider (Criteria criteria, boolean enabledOnly) •Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); criteria.setPowerRequirement(Criteria.POWER_LOW);

Get the Street Address from Location information
Geocoder gc = new Geocoder(getApplicationContext()); try {

List<Address> lst =
gc.getFromLocation( myLocation.getLatitude(), myLocation.getLongitude(), 1 ); }//lst.get(0).getAddressLine(0)

Locations Docs

Content Providers

What is content provider?
• Application Components that manage access to a structured set of data

• standard interface that connects data in one process
with code running in another process • ContentResolver is used to manage data access from ContentProvider • The ContentResolver methods provide the basic "CRUD" (create, retrieve, update, and delete) functions of persistent storage.

What is content provider? (cont.)
• The provider object receives data requests from clients, performs the requested action, and returns the results.

What is content provider? (cont.)
• Android includes content providers that manage data such as audio, video, images, and personal

contact information
• You don't need to develop your own provider if you don't intend to share your data with other applications.

Accessing ConentProviders
• Accessing Data from content providers is about executing a query against a specific URI, – specify columns – selection criteria (WHERE clause) – how you want the results ordered. • The built in providers define constants that you can use to specify URIs. • Examples are contact data or the audio files – android.provider.ContactsContract.Contacts.ContentURI – android.provider.MediaStore.Audio.EXTERNAL_CONTENT_URI

Accessing Content Providers data
Cursor mCursor = getContentResolver().query

ContactsContract.Contacts.CONTENT_URI, projection, SelectionClause, SelectionArgs, Order );

Accessing Content Providers data
Cursor mCursor = managedQuery

ContactsContract.Contacts.CONTENT_URI, projection, SelectionClause, SelectionArgs, Order );

Contacts Provider example
• Setting up parameters : projection // A "projection" defines the columns that will be returned for each row

String[] projection = new String[]
{ ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.TIMES_CONTACTED, ContactsContract.Contacts.LAST_TIME_CONTACTED, ContactsContract.Contacts.STARRED };

Contacts Provider example (cont.)
• Selection Clause and arguments – // Constructs a selection clause by concatenating the user's

input to the column name String
mSelectionClause = “Name = Mohammed" ; – // Constructs a selection clause with a replaceable parameter String mSelectionClause = " Name = ?"; // Defines an array to contain the selection arguments String[] selectionArgs = {“Mohammed"};

Contacts Provider example (cont.)
• Order – Represents ordered by

– E.g.
ContactsContract.Contacts.LAST_TIME_CONTACTED + " DESC"

Content Provider Permissions