android:name="android.permission.ACCESS_FINE_LOCATION"/>
• If you want to develop a specific class for your own GPS based application, other
than default class of MainActivity, you should extend the new class from Service
and also implements LocationListener. For example:
public class GPSReader extends Service implements LocationListener.
• In order to get the location service of mobile phone, you should use this line of
code:
locationManager =
(LocationManager)mContext.getSystemService(LOCATION_SERVICE);
Which mContext is your instance from mainactivity class. As you want to call
GPS reader from mainactivity class, it would be better to develop a method for
your GPSReader class to get the location.
(c) 2019, University of New South Wales, Australia. Page 2
• Getting GPS Status:
isGPSEnabled =
locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
• Get Location from GPS Provider:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES,
this)
location =
locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER)
• Get Location from Network Provider:
Location=
locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVI
DER);
• After obtaining the location, you can use methods of class Location such as
getAltitude() , getLongitude(), … to obtain the details of detected position.
• In main activity class you can create an instance of GPS class as :
GPSReader GP = new GPSReader(this);
More details and samples:
http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/
https://sites.google.com/site/androidhowto/how-to-1/using-the-gps
Lab Task
Task1 (0.5 Marks): Enabling GPS
Develop a simple GPS application program that can check whether GPS is active or not
(Figure1).
Optional task: If GPS is not enabled you can ask user to direct him to setting in order to
enable GPS (Figure2). You can use alertDialog class to create a dialog:
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
To direct user to the setting of mobile phone, you can get an instance of Intent class as :
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
(c) 2019, University of New South Wales, Australia. Page 3
Task2 (0.5 Marks): A simple GPS reader
Extend the previous application of task1 that can read position data from the GPS and
Network Provider and show the results on the screen after pressing a button by user (such as
Figure 3).
Task3 (1 Marks): A simple GPS Tracker
Develop a simple GPS based application that can give your position and speed in any
arbitrary time when you are moving (Figure 4).
Note1: In order to develop a location sensitive program you have to override the
onLocationChanged(Location l) method from LocationManager class.
Note2: In order to convert GPS time format to a usual format like YYYY/MM/DD you can
use SimpleDateFormat class as:
long time = loc.getTime();
Date date = new Date(time);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String textofDate = sdf.format(date);
Figure1 Figure2
(c) 2019, University of New South Wales, Australia. Page 4
Figure 3 Figure 4