Request Place Updates Amongst Locationlistener.Onlocationchanged()

Posted on

This instance present how to asking place machine updates, yesteryear implementing LocationListener.onLocationChanged().


To live Google Play Service inward your project, y’all accept to Add Google Play Services to Android Studio project.

MainActivity.java

package com.blogspot.android_er.androidautoupdatelocation;  import android.Manifest; import android.content.pm.PackageManager; import android.location.Location; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.ActivityCompat; import android.support.v7.app.AppCompatActivity; 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.LocationListener; import com.google.android.gms.location.LocationRequest; import com.google.android.gms.location.LocationServices;  import java.text.DateFormat;  populace course of report MainActivity extends AppCompatActivity         implements LocationListener, GoogleApiClient.ConnectionCallbacks,         GoogleApiClient.OnConnectionFailedListener {      GoogleApiClient mGoogleApiClient;     LocationRequest mLocationRequest;     TextView textAutoUpdateLocation;      static concluding int MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         textAutoUpdateLocation = (TextView) findViewById(R.id.autoupdatelocation);          mLocationRequest = novel LocationRequest();         mLocationRequest.setInterval(5000);         mLocationRequest.setFastestInterval(5000);         mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);         mGoogleApiClient = novel GoogleApiClient.Builder(this)                 .addConnectionCallbacks(this)                 .addOnConnectionFailedListener(this)                 .addApi(LocationServices.API)                 .build();     }      @Override     protected void onStart() {         mGoogleApiClient.connect();         super.onStart();     }      @Override     protected void onStop() {         mGoogleApiClient.disconnect();         super.onStop();     }      @Override     populace void onLocationChanged(Location location) {         if (location != null) {             String strLocation =                     DateFormat.getTimeInstance().format(location.getTime()) + "\n" +                             "Latitude=" + location.getLatitude() + "\n" +                             "Longitude=" + location.getLongitude();             textAutoUpdateLocation.setText(strLocation);         }     }      @Override     populace void onConnected(@Nullable Bundle bundle) {          if (ActivityCompat.checkSelfPermission(this,                     Manifest.permission.ACCESS_FINE_LOCATION)                     != PackageManager.PERMISSION_GRANTED                 && ActivityCompat.checkSelfPermission(this,                     Manifest.permission.ACCESS_COARSE_LOCATION)                     != PackageManager.PERMISSION_GRANTED) {             // TODO: Consider calling             //    ActivityCompat#requestPermissions             // hither to asking the missing permissions, too and thus overriding             //   populace void onRequestPermissionsResult(int requestCode, String[] permissions,             //                                          int[] grantResults)             // to cause got the instance where the user grants the permission. See the documentation             // for ActivityCompat#requestPermissions for to a greater extent than details.             ActivityCompat.requestPermissions(MainActivity.this,                     novel String[]{Manifest.permission.ACCESS_FINE_LOCATION},                     MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);              return;         }         LocationServices.FusedLocationApi.requestLocationUpdates(                 mGoogleApiClient, mLocationRequest, this);      }      @Override     populace void onRequestPermissionsResult(             int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {         switch (requestCode) {             instance MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: {                 // If asking is cancelled, the consequence arrays are empty.                 if (grantResults.length > 0                         && grantResults[0] == PackageManager.PERMISSION_GRANTED) {                     Toast.makeText(MainActivity.this,                             "permission was granted, :)",                             Toast.LENGTH_LONG).show();                      try{                         LocationServices.FusedLocationApi.requestLocationUpdates(                                 mGoogleApiClient, mLocationRequest, this);                     }catch(SecurityException e){                         Toast.makeText(MainActivity.this,                                 "SecurityException:\n" + e.toString(),                                 Toast.LENGTH_LONG).show();                     }                 } else {                     Toast.makeText(MainActivity.this,                             "permission denied, ...:(",                             Toast.LENGTH_LONG).show();                 }                 return;             }         }     }      @Override     populace void onConnectionSuspended(int i) {      }      @Override     populace 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.androidautoupdatelocation.MainActivity">       <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center_horizontal"         android:autoLink="web"         android:text="https://rootandroid.org//"         android:textStyle="bold" />      <TextView         android:id="@+id/autoupdatelocation"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:textSize="30dp"         android:textStyle="italic|bold"/> </LinearLayout>  

Add uses-permission of “android.permission.ACCESS_FINE_LOCATION” inward AndroidManifest.xml

 This instance present how to asking place machine updates Request Location Updates amongst LocationListener.onLocationChanged()Download the files .

It’s non consummate solution, to know to a greater extent than details, cite Android Developers – Receiving Location Updates, a instance of “LocationUpdates” is provided.


Related:
– Get my Last Known Location, yesteryear calling LocationServices.FusedLocationApi.getLastLocation()