title: Getting the Last known location application,Android
body: <p>I am trying this simple application(<a href="https://github.com/googlesamples/android-play-location/tree/master/BasicLocationSample" rel="nofollow">https://github.com/googlesamples/android-play-location/tree/master/BasicLocationSample</a>) which retrieve the last known location of user.</p>
  
  <p>MainActivity.java</p>
  
  <pre><code>package com.example.dell.locationapplication;
  
  import android.support.v7.app.ActionBarActivity;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuItem;
  
  /**
   * Copyright 2014 Google Inc. All Rights Reserved.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  
  import android.location.Location;
  import android.os.Bundle;
  import android.support.v7.app.ActionBarActivity;
  import android.util.Log;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import com.google.android.gms.common.ConnectionResult;
  import com.google.android.gms.common.api.GoogleApiClient;
  import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
  import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
  import com.google.android.gms.location.LocationServices;
  
  /**
   * Location sample.
   *
   * Demonstrates use of the Location API to retrieve the last known location for a device.
   * This sample uses Google Play services (GoogleApiClient) but does not need to authenticate a user.
   * See https://github.com/googlesamples/android-google-accounts/tree/master/QuickStart if you are
   * also using APIs that need authentication.
   */
  public class MainActivity extends ActionBarActivity implements
          ConnectionCallbacks, OnConnectionFailedListener {
  
      protected static final String TAG = "basic-location-sample";
  
      /**
       * Provides the entry point to Google Play services.
       */
      protected GoogleApiClient mGoogleApiClient;
  
      /**
       * Represents a geographical location.
       */
      protected Location mLastLocation;
  
      protected TextView mLatitudeText;
      protected TextView mLongitudeText;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          mLatitudeText = (TextView) findViewById((R.id.latitude_text));
          mLongitudeText = (TextView) findViewById((R.id.longitude_text));
  
          buildGoogleApiClient();
      Log.d("hh","gg");
      }
  
      /**
       * Builds a GoogleApiClient. Uses the addApi() method to request the LocationServices API.
       */
      protected synchronized void buildGoogleApiClient() {
          mGoogleApiClient = new GoogleApiClient.Builder(this)
                  .addConnectionCallbacks(this)
                  .addOnConnectionFailedListener(this)
                  .addApi(LocationServices.API)
                  .build();
      }
  
      @Override
      protected void onStart() {
          super.onStart();
          mGoogleApiClient.connect();
      }
  
      @Override
      protected void onStop() {
          super.onStop();
          if (mGoogleApiClient.isConnected()) {
              mGoogleApiClient.disconnect();
          }
      }
  
      /**
       * Runs when a GoogleApiClient object successfully connects.
       */
      @Override
      public void onConnected(Bundle connectionHint) {
          // Provides a simple way of getting a device's location and is well suited for
          // applications that do not require a fine-grained location and that do not need location
          // updates. Gets the best and most recent location currently available, which may be null
          // in rare cases when a location is not available.
          mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
          if (mLastLocation != null) {
              mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude()));
              mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude()));
          } else {
              Toast.makeText(this, R.string.no_location_detected, Toast.LENGTH_LONG).show();
          }
      }
  
      @Override
      public void onConnectionFailed(ConnectionResult result) {
          // Refer to the javadoc for ConnectionResult to see what error codes might be returned in
          // onConnectionFailed.
          Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + result.getErrorCode());
      }
  
  
      @Override
      public void onConnectionSuspended(int cause) {
          // The connection to Google Play services was lost for some reason. We call connect() to
          // attempt to re-establish the connection.
          Log.i(TAG, "Connection suspended");
          mGoogleApiClient.connect();
      }
  }
  </code></pre>
  
  <p>AndroidManifest.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.dell.locationapplication"
      android:versionCode="1"
      android:versionName="1.0" &gt;
  
      &lt;uses-sdk
          android:minSdkVersion="15"
          android:targetSdkVersion="21" /&gt;
  
      &lt;uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
          &lt;meta-data
              android:name="com.google.android.gms.version"
              android:value="@integer/google_play_services_version" /&gt;
  
          &lt;activity
              android:name="com.example.dell.locationapplication.MainActivity"
              android:label="@string/app_name" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>build.gradle ( In my Android Studio Project)</p>
  
  <pre><code>apply plugin: 'com.android.application'
  
  android {
      compileSdkVersion 21
      buildToolsVersion "21.1.2"
      defaultConfig {
          applicationId "com.example.dell.locationapplication"
          minSdkVersion 15
          targetSdkVersion 21
          versionCode 1
          versionName "1.0"
      }
      buildTypes {
          release {
              minifyEnabled false
              proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
          }
      }
      productFlavors {
      }
  }
  
  dependencies {
      compile fileTree(dir: 'libs', include: ['*.jar'])
      compile 'com.android.support:appcompat-v7:21.0.3'
      compile 'com.google.android.gms:play-services:7.3.0'
  }
  </code></pre>
  
  <p>I am trying to run it on XOLO Q700, Android Version 4.2.1( API 17 ) and Google Play Services version on my device is 7.0.99.</p>
  
  <p>The application is intended to display the latitude and longitude of user last known location but it's not displaying anything on my device.</p>
  
  <p>I am new to Android.Kindly help me on how to solve this issue.</p>