reference: Android Developers - Getting the Last Known Location
To use Google Play Service in your project, you have to Add Google Play Services to Android Studio project, refer last post.
MainActivity.java
package com.blogspot.android_er.androidgetlastlocation;
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
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.location.LocationServices;
public class MainActivity extends AppCompatActivity
implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
Button btnGetLastLocation;
TextView textLastLocation;
GoogleApiClient mGoogleApiClient;
Location mLastLocation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnGetLastLocation = (Button) findViewById(R.id.getlastlocation);
btnGetLastLocation.setOnClickListener(btnGetLastLocationOnClickListener);
textLastLocation = (TextView) findViewById(R.id.lastlocation);
// Create an instance of GoogleAPIClient.
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
}
View.OnClickListener btnGetLastLocationOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mGoogleApiClient != null){
if(mGoogleApiClient.isConnected()){
getMyLocation();
}else{
Toast.makeText(MainActivity.this,
"!mGoogleApiClient.isConnected()", Toast.LENGTH_LONG).show();
}
}else{
Toast.makeText(MainActivity.this,
"mGoogleApiClient == null", Toast.LENGTH_LONG).show();
}
}
};
private void getMyLocation(){
try{
/* code should explicitly check to see if permission is available
(with 'checkPermission') or explicitly handle a potential 'SecurityException'
*/
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLastLocation != null) {
textLastLocation.setText(
String.valueOf(mLastLocation.getLatitude()) + "\n"
+ String.valueOf(mLastLocation.getLongitude()));
Toast.makeText(MainActivity.this,
String.valueOf(mLastLocation.getLatitude()) + "\n"
+ String.valueOf(mLastLocation.getLongitude()),
Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this,
"mLastLocation == null",
Toast.LENGTH_LONG).show();
}
} catch (SecurityException e){
Toast.makeText(MainActivity.this,
"SecurityException:\n" + e.toString(),
Toast.LENGTH_LONG).show();
}
}
@Override
protected void onStart() {
mGoogleApiClient.connect();
super.onStart();
}
@Override
protected void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}
@Override
public void onConnected(@Nullable Bundle bundle) {
getMyLocation();
}
@Override
public void onConnectionSuspended(int i) {
Toast.makeText(MainActivity.this,
"onConnectionSuspended: " + String.valueOf(i),
Toast.LENGTH_LONG).show();
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Toast.makeText(MainActivity.this,
"onConnectionFailed: \n" + connectionResult.toString(),
Toast.LENGTH_LONG).show();
}
}
layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:orientation="vertical"
tools:context="com.blogspot.android_er.androidgetlastlocation.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />
<Button
android:id="@+id/getlastlocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Get my last location"/>
<TextView
android:id="@+id/lastlocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Permission:
Add uses-permission of ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION in your AndroidManifest.xml. In this example, I use ACCESS_FINE_LOCATION. (Read remark below about getLastLocation() return null)
remark: getLastLocation() return null!
- When run on real device, ASUS Zenfone 2 running Android 5.0:
According to Android Developers - Getting the Last Known Location, you can add permission of ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION. But in my trial, if add permission of ACCESS_COARSE_LOCATION, getLastLocation() return null mostly, around 2 success in more than 30 try. if add permission of ACCESS_FINE_LOCATION, it almost success every time.
Next:
- Requesting Permissions of Manifest.permission.ACCESS_FINE_LOCATION at Run Time
Related:
- Request Location Updates with LocationListener.onLocationChanged()
1 comment:
Is fantastic, but does not work with real app and as I add permissions, thanks
Post a Comment