How to check when LocationServices.FusedLocationApi.getLastLocation() is not null?
<p>I'm first checking if Location is ON using Android LocationServices.SettingsApi, if it's not I present standard location setting dialog using <a href="http://developer.android.com/training/location/change-location-settings.html#prompt" rel="nofollow">Android documentation example</a>. Later if user enables the Location I use <em>LocationServices.FusedLocationApi.getLastLocation()</em> to get current location. Here as below:</p>

<pre><code>protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == ENABLE_LOCATION_REQUEST_CODE) {
        switch (resultCode) {
            case Activity.RESULT_OK:
                Location location = LocationServices.FusedLocationApi
                                       .getLastLocation(mGoogleApiClient);
                while (location == null) {
                    location = LocationServices.FusedLocationApi
                        .getLastLocation(mGoogleApiClient);
                }
                break;
            case Activity.RESULT_CANCELED:
                Toast.makeText(this, "Location not enabled!", Toast.LENGTH_LONG).show();
                break;
            default:
                break;
        }
    }
}
</code></pre>

<p><em>LocationServices.FusedLocationApi
                            .getLastLocation()</em> takes around 1 second in my device to return non null value after enabling Location. The problem is that I'm using while statement till <em>LocationServices.FusedLocationApi
                            .getLastLocation()</em> returns non null value. I think the way I'm doing this in UI Thread is not right. Maybe I can do it in background thread but what I want to know is there some better native way of doing.</p>