Location Awareness in Android: How to Track the Last Known Location

 

In this world with such an advancement that almost half of the population uses a mobile phone. Out of which billions use Android phones in their day to day life. Mobile phones have become such a necessity that generally, the users carry their phone everywhere.

Also, the Enterprise mobile app development is becoming the first choice for every entrepreneur. These days Android developers develop apps it in such a way that user’s location information can be easily found by them. Knowing your user’s location is a useful information, being an admin of the mobile app. When you go to play store, you’ll find several apps that are entirely location based that makes our lives easier. Google Maps, Uber and a lot more, who have made the lives of the mobile phone users easier than ever. You can now roam around at any place even if are unaware of the roads.

Google maps will guide you everywhere. Location awareness is a very important feature in the Android applications. With the help of Google Play services location APIs, the app developed by you can request the last known location of the user’s device. As developers, you can capitalize on providing a contextual experience depending on their current location, if you are providing the location-based service.

Even if you are interested in knowing user’s current location, you can easily find it as it is almost equivalent to the last known location of the device. For precise results, you can use the fused location provider in order to retrieve the last known location of the device.

This is a location provider from the location APIs in Google Play services. It manages the entire location technology and hence provides simple API so that specific requirements can be met at a high level.

This can also optimize the battery usage of the device. Location awareness is one of the most important features of the mobile devices. Below, you will find various steps how to make a request for the location of a device with the help of getLastLocation()

  • Set Up Google Play Services

When you need to access the fused location provider, make sure that the development project of your application has included Google Play services. If not, download and install the Google Play services component through the SDK Manager and post that, add the library to the project.

  • Specify App Permissions

If you are developing an application that uses location service, your application must request location permissions.

Android offers two kinds of location permissions.i.e. ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION.

It depends on the permission chosen by you whether the location returned by the API of the app will be accurate or not.

If the location permission ACCESS_COARSE_LOCATION is chosen, the API would return a location with an accuracy almost equivalent to the city block.

Using the following snippet code as the user-permission element in the app manifest as requesting is necessary on the course locations:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.google.android.gms.location.sample.basiclocationsample" >

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

</manifest>
  • Creation of Location Services Client

In this method, you need to create an instance of the fused location provider client when the following snippet code shows up:

private FusedLocationProviderClient mFusedLocationClient;

// ..

@Override

protected void onCreate(Bundle savedInstanceState) {

    // ...

    mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);

}
  • Get the Last Known Locations

Post the creation of the Location Services client, it is easy to get the last known location of the user’s device. After connecting your app with these, it is easy to use fused location provider method to retrieve the device location.

To request the last known location, call the getLastLocation() method. The following code snippet illustrates the request and a simple handling of the response:

mFusedLocationClient.getLastLocation()

        .addOnSuccessListener(this, new OnSuccessListener<Location>() {

            @Override

            public void onSuccess(Location location) {

                // Got last known location. In some rare situations, this can be null.

                if (location != null) {

                    // Logic to handle location object

                }

            }

        });

This method returns a task that can be used to get a location object along with the latitude and longitude coordinates. You will not get the location object, in certain situations.

  • When the location has been turned off from the settings in the device.
  • When the device has never recorded the location, that can be possible when the device is new or has restored to factory settings.
  • Google Play services on the device have already restarted.

Location awareness is a necessary and a very important part of your android application. With this, you might be thinking that there is a lot of work to be done for getting a user’s location. But, once you complete it, you be having a successful working model which can be used at any time you require to access the location of the user.

Generally, not everyone likes to work with such activities that have so many interfaces and tries to make the processes as short as possible.

Author
The author is Harshal Shah, CEO and founder of Elsner Technologies, a professional Android App Development Company & global IT consulting firm. He is a tech evangelist with a reputation to provide optimum solutions for business automation and solving real-life problems with the power of IT.