I have copied the below code from google (don't remember the URL) which runs fine on command click listner event , i want to know how can i use/call the same java class (GoogleplayServiceLocation) from my MainActivity class
package com.dbprox.tagpic;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
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.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
/**
* Created by user on 24/1/2016.
*/
public class GoogleplayServiceLocation extends Activity implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,LocationListener{
TextView TxtViewlat;
TextView TxtViewlon;
// private final Context context;
private final static int PLAY_SERVICES_RESOLUTION_REQUEST=1000;
private Location mLastLocation;
private GoogleApiClient mGoogleApliClient;
private boolean mRequestLocationUpdates=false;
private LocationRequest mLocationRequest;
private static int UPDATE_INTERVAL=10000;
private static int FASTEST_INTTERVAL=5000;
private static int DISPLACEMENT =10;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(checkPlayServices()){
buildGoogleApiClient();
createLocationRequest();
displayLocation();
}
}
public void GoogleplayServiceLocations (Context context)
{
// this.context=context;
if(checkPlayServices()){
buildGoogleApiClient();
createLocationRequest();
displayLocation();
}
}
#Override
protected void onStart()
{
super.onStart();
if(mGoogleApliClient!=null)
{mGoogleApliClient.connect();}
}
#Override
protected void onResume()
{
super.onResume();
checkPlayServices();
if(mGoogleApliClient.isConnected() && mRequestLocationUpdates ){
startLocationUpdates();
}
}
#Override
protected void onStop()
{
super.onStop();
if(mGoogleApliClient.isConnected())
{
mGoogleApliClient.disconnect();
}
}
#Override
protected void onPause(){
super.onPause();
stopLocationUpdates();
}
private void displayLocation(){
mLastLocation= LocationServices.FusedLocationApi.getLastLocation(mGoogleApliClient);
if(mLastLocation!=null) {
double latitude = mLastLocation.getLatitude();
double longitude = mLastLocation.getLongitude();
Toast.makeText(getApplicationContext(),
latitude + " - " + longitude , Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(),"Can'rt connect to location", Toast.LENGTH_LONG).show();
}
}
private void togglePeriodLocationUpdates(){
if(!mRequestLocationUpdates) {
mRequestLocationUpdates=true;
startLocationUpdates();
}
else {
mRequestLocationUpdates=false;
stopLocationUpdates();
}
}
protected synchronized void buildGoogleApiClient(){
mGoogleApliClient=new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API).build();
}
protected void createLocationRequest(){
mLocationRequest=new LocationRequest();
mLocationRequest.setInterval(UPDATE_INTERVAL);
mLocationRequest.setFastestInterval(FASTEST_INTTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setSmallestDisplacement(DISPLACEMENT);
displayLocation();
}
private boolean checkPlayServices() {
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (resultCode != ConnectionResult.SUCCESS) {
if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
GooglePlayServicesUtil.getErrorDialog(resultCode, this, PLAY_SERVICES_RESOLUTION_REQUEST).show();
} else {
Toast.makeText(getApplicationContext(), "This device is not supported", Toast.LENGTH_LONG).show();
finish();
}
return true;
}
return false;
}
protected void startLocationUpdates(){
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApliClient, mLocationRequest, (com.google.android.gms.location.LocationListener) this);
}
protected void stopLocationUpdates(){
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApliClient, (com.google.android.gms.location.LocationListener) this);
}
#Override
public void onLocationChanged(Location location) {
mLastLocation=location;
Toast.makeText(getApplicationContext(),"Location Changed",Toast.LENGTH_LONG).show();
displayLocation();
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onConnected(Bundle bundle) {
displayLocation();
if(mRequestLocationUpdates){
startLocationUpdates();
}
}
#Override
public void onConnectionSuspended(int i) {
mGoogleApliClient.connect();
}
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d("TAG", "Connection Failed: " + connectionResult.getErrorCode());
}
}
Related
So, I have custom Location Service class, from which I want to get last known location. It's possible, that I can call getLastKnownLocation() before GoogleApiClient is connected, so I have to wait for it and then call getLastKnownLocation(), but I have no clue how to manage that. I'm thinking that RxJava 2 can help me with that, but I'm not familiar with that framework yet. This is my class for now:
import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
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 com.google.android.gms.maps.model.LatLng;
import javax.inject.Inject;
import pl.pancor.android.air.base.FragmentScope;
#FragmentScope
public class LocationService implements Location.Service,
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,
ActivityCompat.OnRequestPermissionsResultCallback {
private static final String TAG = LocationService.class.getSimpleName();
private static final int PERMISSIONS_REQUEST = 13;
private GoogleApiClient mGoogleApiClient;
private Activity mActivity;
private android.location.Location mLastLocation;
private Location.Receiver mReceiver;
#Inject
LocationService(Activity activity) {
mActivity = activity;
}
#Override
public void getLastKnownLocation() {
if (isPermissionsGranted(true))
getLocation();
}
/**
* #param request if permissions aren't granted and {#param request} is true,
* then request permissions
* #return true if location permissions are granted
*/
private boolean isPermissionsGranted(boolean request) {
if (ActivityCompat.checkSelfPermission(mActivity,
Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(mActivity,
Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
if (request) {
ActivityCompat.requestPermissions(mActivity,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION},
PERMISSIONS_REQUEST);
}
return false;
}
return true;
}
private void getLocation() {
if (mGoogleApiClient != null)
mLastLocation = LocationServices.FusedLocationApi
.getLastLocation(mGoogleApiClient);
if (mLastLocation != null) {
LatLng latLng = new LatLng(mLastLocation.getLatitude(),
mLastLocation.getLongitude());
mReceiver.lastKnownLocation(latLng);
} else {
Log.e(TAG, "NULLLLLLLLLLLLLLLLLLLLLLL");
}
}
#Override
public void onConnected(#Nullable Bundle bundle) {
}
#Override
public void onConnectionSuspended(int i) {
}
#Override
public void onConnectionFailed(#NonNull ConnectionResult connectionResult) {
}
#Override
public void setupReceiver(Location.Receiver receiver) {
mReceiver = receiver;
}
#Override
public void onStart() {
if (mGoogleApiClient != null){
mGoogleApiClient.connect();
} else {
mGoogleApiClient = getGoogleApiClient();
mGoogleApiClient.connect();
}
}
#Override
public void onStop() {
if (mGoogleApiClient != null)
mGoogleApiClient.disconnect();
}
private GoogleApiClient getGoogleApiClient(){
return new GoogleApiClient.Builder(mActivity)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions,
#NonNull int[] grantResults) {
switch (requestCode){
case PERMISSIONS_REQUEST:
if (grantResults.length > 0 &&
grantResults[0] == PackageManager.PERMISSION_GRANTED){
getLastKnownLocation();
} else {
}
}
}
}
I need somehow to check if mGoogleApiClient is connected (mGoogleApiClient.isConnected()) and if not, wait to connect, and then get location from FusedLocationApi, but I don't want to put methods to onConnected(), because it will sometimes return location, when I don't want to return location.
After your explanations in the comment section, I would do something like this:
in the Receiver/Fragment class I would put some logic that sets a variable "updateUI" to true in the LocationService class, when it is appropriate for onConnected to call the mReceiver.lastKnownLocation(latLng)
method. The default value will be false, and if onConnected gets called before the receiver is ready, the method mReceiver.lastKnownLocation(latLng)
won't be called.
Another approach is to store always the last known location in your SharedPreferences (or at least in the onPause method). Then, you can always use it the first time you need a location and wait for more precise location later, but this method won't be so precise at start time.
So, after some time, i manage to make it and also finished my entire class and i would like to share with you, what i did
public interface Location {
interface Service extends BaseLocation<Receiver>{
void onStart();
void onStop();
void onActivityResult(int requestCode, int resultCode);
void getLastKnownLocation();
}
interface Receiver{
void lastKnownLocation(double latitude, double longitude);
void userRefusedToSendLocation();
void unableToObtainLocation();
}
}
import android.Manifest;
import android.app.Activity;
import android.content.IntentSender;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.LocationSettingsRequest;
import com.google.android.gms.location.LocationSettingsResult;
import com.google.android.gms.location.LocationSettingsStatusCodes;
import javax.inject.Inject;
import pl.pancor.android.air.base.FragmentScope;
#FragmentScope
public class LocationService implements Location.Service,
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,
ActivityCompat.OnRequestPermissionsResultCallback, LocationListener,
ResultCallback<LocationSettingsResult>{
private static final int PERMISSIONS_REQUEST = 13;
private static final int SETTINGS_CHECK = 23;
private static final int GOOGLE_API_CLIENT_ERROR = 33;
private static final int LOCATION_EXPIRATION_TIME = 10 * 1000;
private static final int LOCATION_INTERVAL = 2 * 1000;
private GoogleApiClient mGoogleApiClient;
private Activity mActivity;
private LocationRequest mLocationRequest;
private android.location.Location mLastLocation;
private Location.Receiver mReceiver;
private Handler mHandler;
private final Runnable mExpiredLocationUpdate = new Runnable() {
#Override
public void run() {
mReceiver.unableToObtainLocation();
}
};
private boolean isWaitingForConnect = false;
#Inject
LocationService(Activity activity) {
mActivity = activity;
}
#Override
public void getLastKnownLocation() {
if (isPermissionsGranted(true))
checkLocationSettings();
}
#Override
public void onActivityResult(int requestCode, int resultCode) {
resolveProblems(requestCode, resultCode);
}
#Override
public void onLocationChanged(android.location.Location location) {
if (mLastLocation == null) {
mLastLocation = location;
sendLatLngToReceiver();
}
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
mHandler.removeCallbacks(mExpiredLocationUpdate);
}
#Override
public void onConnected(#Nullable Bundle bundle) {
if (isWaitingForConnect)
getLastKnownLocation();
}
#Override
public void onConnectionSuspended(int i) {
//mGoogleApiClient will automatically try to reconnect
}
#Override
public void onConnectionFailed(#NonNull ConnectionResult result) {
if (!result.hasResolution()){
mReceiver.unableToObtainLocation();
GoogleApiAvailability.getInstance()
.getErrorDialog(mActivity, result.getErrorCode(), 0).show();
return;
}
if (mActivity.hasWindowFocus()) {
try {
result.startResolutionForResult(mActivity, GOOGLE_API_CLIENT_ERROR);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
}
}
#Override
public void setupReceiver(Location.Receiver receiver) {
mReceiver = receiver;
}
#Override
public void onStart() {
mHandler = new Handler();
if (mGoogleApiClient != null){
mGoogleApiClient.connect();
} else {
mGoogleApiClient = getGoogleApiClient();
mGoogleApiClient.connect();
}
}
#Override
public void onStop() {
if (mGoogleApiClient != null) {
LocationServices.FusedLocationApi.removeLocationUpdates(
mGoogleApiClient, this);
mGoogleApiClient.disconnect();
}
mHandler.removeCallbacks(mExpiredLocationUpdate);
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions,
#NonNull int[] grantResults) {
switch (requestCode){
case PERMISSIONS_REQUEST:
if (grantResults.length > 0 &&
grantResults[0] == PackageManager.PERMISSION_GRANTED){
getLastKnownLocation();
} else {
mReceiver.userRefusedToSendLocation();
}
}
}
#Override
public void onResult(#NonNull LocationSettingsResult result) {
final Status status = result.getStatus();
switch (status.getStatusCode()){
case LocationSettingsStatusCodes.SUCCESS:
getLocation();
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
if (mActivity.hasWindowFocus()) {
try {
status.startResolutionForResult(mActivity, SETTINGS_CHECK);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
mReceiver.unableToObtainLocation();
break;
}
}
private void checkLocationSettings() {
if (mGoogleApiClient != null){
mLocationRequest = new LocationRequest()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setFastestInterval(LOCATION_INTERVAL / 2)
.setInterval(LOCATION_INTERVAL)
.setNumUpdates(1);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(mLocationRequest);
PendingResult<LocationSettingsResult> result = LocationServices
.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());
result.setResultCallback(this);
}
}
private void getLocation(){
if (mGoogleApiClient != null)
mLastLocation = LocationServices.FusedLocationApi
.getLastLocation(mGoogleApiClient);
sendLatLngToReceiver();
}
private void sendLatLngToReceiver(){
if (mLastLocation != null) {
mReceiver.lastKnownLocation(mLastLocation.getLatitude(),
mLastLocation.getLongitude());
mHandler.removeCallbacks(mExpiredLocationUpdate);
} else {
requestLocation();
}
}
private void requestLocation(){
if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
mHandler.postDelayed(mExpiredLocationUpdate, LOCATION_EXPIRATION_TIME);
} else {
isWaitingForConnect = true;
}
}
/**
* #param request if permissions aren't granted and {#param request} is true,
* then request permissions
* #return true if location permissions are granted
*/
private boolean isPermissionsGranted(boolean request) {
if (ActivityCompat.checkSelfPermission(mActivity,
Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(mActivity,
Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
if (request) {
ActivityCompat.requestPermissions(mActivity,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION},
PERMISSIONS_REQUEST);
}
return false;
}
return true;
}
private GoogleApiClient getGoogleApiClient(){
return new GoogleApiClient.Builder(mActivity)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
private void resolveProblems(int requestCode, int resultCode){
switch (requestCode){
case SETTINGS_CHECK:
switch (resultCode){
case Activity.RESULT_OK:
getLastKnownLocation();
break;
case Activity.RESULT_CANCELED:
mReceiver.userRefusedToSendLocation();
break;
}
break;
case GOOGLE_API_CLIENT_ERROR:
switch (resultCode) {
case Activity.RESULT_OK:
mGoogleApiClient.connect();
break;
case Activity.RESULT_CANCELED:
mReceiver.unableToObtainLocation();
break;
}
}
}
}
I use the code below in my project to retrieve the current location of the user. However, this seems to work on my test devices of API Level 23, but does not work on my test devices of API levels 19 to 22. The request for a location will return null and therefor place me at coordinates 0,0..
How can i get correct coordinates for API levels 19 to 22?
package be.enventorslab.pingvalue;
import android.content.IntentSender;
import android.content.pm.PackageManager;
import android.location.Location;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
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 com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import be.enventorslab.pingvalue.functions.Functions;
public class NearbyLocationActivity extends AppCompatActivity
implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener {
public static final String TAG = NearbyLocationActivity.class.getSimpleName();
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
private GoogleMap mMap;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
Bundle bundle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nearby_map);
setUpMapIfNeeded();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(10 * 1000)
.setFastestInterval(1 * 1000)
.setSmallestDisplacement(1);
}
#Override
protected void onStart() {
super.onStart();
setUpMapIfNeeded();
mGoogleApiClient.connect();
}
#Override
protected void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
mGoogleApiClient.disconnect();
}
}
private void setUpMapIfNeeded() {
if (mMap == null) {
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
}
}
#Override
public void onConnected(Bundle bundle) {
if (Functions.Permissions.getFineLocation(this)) {
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (location == null) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
} else {
handleNewLocation(location);
}
} else {
this.bundle = bundle;
}
}
#Override
public void onConnectionSuspended(int i) {
}
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
if (connectionResult.hasResolution()) {
try {
connectionResult.startResolutionForResult(this, CONNECTION_FAILURE_RESOLUTION_REQUEST);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
} else {
Log.i(TAG, "Location services connection failed with code " + connectionResult.getErrorCode());
}
}
#Override
public void onLocationChanged(Location location) {
handleNewLocation(location);
}
private void handleNewLocation(Location location) {
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
MarkerOptions options = new MarkerOptions().position(latLng).title("I am here!");
mMap.addMarker(options);
}
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
if (requestCode == Functions.Permissions.MY_PERMISSIONS_FINE_LOCATION) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
onConnected(bundle);
}
}
}
}
For some reason it only updates the textviews when the app hits onPause, like when I hit the home button, or multitasking button. Can someone help me figure out why that is?
MainActivity.java:
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private String lat, lon;
private TextView longTextView, latTextView;
LocationService locationService = new LocationService(this);
private Intent intentService;
private PendingIntent pendingIntent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
latTextView = (TextView) findViewById(R.id.latitude_textview);
longTextView = (TextView) findViewById(R.id.longitude_textview);
}
#Override
protected void onStart() {
super.onStart();
locationService.buildGoogleApiClient();
locationService.apiConnect();
if (latTextView != null && longTextView != null) {
latTextView.setText( locationService.getLat());
longTextView.setText( locationService.getLon());
Toast.makeText(getApplicationContext(), " Actually got location", Toast.LENGTH_SHORT)
.show();
} else {
Toast.makeText(getApplicationContext(), "The shit was null fam", Toast.LENGTH_LONG)
.show();
}
}
#Override
protected void onStop() {
super.onStop();
locationService.apiDisconnect();
}
}
LocationService.java:
import android.Manifest;
import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.FusedLocationProviderApi;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import static com.google.android.gms.wearable.DataMap.TAG;
public class LocationService extends Service implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener, LocationListener {
// ============================================================= Variables
Context context;
Location mLastLocation;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
private String lat, lon;
final static String[] LOCATION_PERMISSIONS = {Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION};
public static final long UPDATE_INTERVAL_IN_MILLISECONDS = 10000;
public static final long UPDATE_FASTEST_INTERVAL_IN_MILLISECONDS = UPDATE_INTERVAL_IN_MILLISECONDS / 2;
public static boolean isEnded = false;
public static Boolean requestingLocationUpdates;
protected String lastUpdateTime;
final int GOOGLEAPI_REQUEST_CODE = 24;
private FusedLocationProviderApi fusedLocationProviderApi = LocationServices.FusedLocationApi;
// ============================================================= Constructor
public LocationService(Context context) {
this.context = context;
}
// ============================================================= Getters / Setters
public String getLon() {
return lon;
}
public void setLon(String lon) {
this.lon = lon;
}
public String getLat() {
return lat;
}
public void setLat(String lat) {
this.lat = lat;
}
// ============================================================= Methods
synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(context)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
public void apiConnect() {
mGoogleApiClient.connect();
}
public void apiDisconnect() {
mGoogleApiClient.disconnect();
}
void updateUI() {
}
// ============================================================= Implemented Location Methods
#Override
public void onLocationChanged(Location location) {
setLat(String.valueOf(location.getLatitude()));
setLon(String.valueOf(location.getLongitude()));
}
#Override
public void onConnectionFailed(#NonNull ConnectionResult connectionResult) {
Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + connectionResult.getErrorCode());
}
#Override
public void onConnected(#Nullable Bundle bundle) {
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS); // Sets Location to update every second
mLocationRequest.setFastestInterval(UPDATE_FASTEST_INTERVAL_IN_MILLISECONDS); // The fastest location can update is every half-second
startLocationUpdates();
// TODO come back to this to see whats up
/* mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);*/
if (mLastLocation != null) {
setLat(String.valueOf(mLastLocation.getLatitude()));
setLon(String.valueOf(mLastLocation.getLongitude()));
}
}
#Override
public void onConnectionSuspended(int i) {
}
protected void startLocationUpdates() {
/*if (!requestingLocationUpdates) {
requestingLocationUpdates = true;*/
if (ActivityCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions((Activity) context, LOCATION_PERMISSIONS, GOOGLEAPI_REQUEST_CODE);
} else {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
Log.i(TAG, " startLocationUpdates===");
isEnded = true;
//}
}
// ============================================================= Implemented Service Methods
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Within {#code onPause()}, we pause location updates, but leave the
// connection to GoogleApiClient intact. Here, we resume receiving
// location updates if the user has requested them.
Log.d("LOC", "Service init...");
isEnded = false;
requestingLocationUpdates = false;
lastUpdateTime = "";
buildGoogleApiClient();
if (mGoogleApiClient.isConnected() && requestingLocationUpdates) {
startLocationUpdates();
}
return Service.START_REDELIVER_INTENT;
}
}
The reason why you are not getting the location updated in textview is because your code doesn't have a way for the service to communicate back to the activity.
If you want to obtain location only when the activity is in foreground don't use Service and please look into this google's example for obtaining location and updating on a TextView using fused location provider.
I am not sure why you are using a Service.Use Service only when you want to continuously fetch the location even when the app is running background.
For this use any one of the method mentioned here to inform the activity that a new location has been obtained.LocalBroadcast would be your best bet. Anyway explore the best possible solution that suits your usecase in the previous link.
I'm a beginner of android programming then sorry if this question could seem silly but I need to understand how to update the view from a class that it is not an activity or a fragment. I have created a class that fetch data from the Google Play services API. I need to redirect this data to the fragment. which are the common software design patterns to achieve it?
This is the code but unfortunately it doesn't work
TodayFragment
package com.salvo.weather.android.fragment;
import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.salvo.weather.android.R;
import com.salvo.weather.android.app.VolleyCallback;
import com.salvo.weather.android.entity.CurrentWeatherEntity;
import com.salvo.weather.android.geolocation.CurrentGeolocation;
/**
* A simple {#link Fragment} subclass.
*/
public class TodayFragment extends Fragment {
private static final String TAG = TodayFragment.class.getSimpleName();
private CurrentGeolocation mCurrentGeolocation;
public TodayFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_today, container, false);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mCurrentGeolocation = CurrentGeolocation.get(getActivity());
}
#Override
public void onStart() {
super.onStart();
mCurrentGeolocation.getmGoogleApiClient().connect();
renderView();
}
#Override
public void onPause() {
super.onPause();
if (mCurrentGeolocation.getmGoogleApiClient().isConnected()) {
mCurrentGeolocation.stopLocationUpdates();
}
}
#Override
public void onResume() {
super.onResume();
if (mCurrentGeolocation.getmGoogleApiClient().isConnected()) {
mCurrentGeolocation.startLocationUpdates();
}
}
#Override
public void onStop() {
super.onStop();
if (mCurrentGeolocation.getmGoogleApiClient().isConnected()) {
mCurrentGeolocation.stopLocationUpdates();
}
}
public void renderView() {
// check if googleApiClient is connected
if (mCurrentGeolocation.getmGoogleApiClient().isConnected()) {
mCurrentGeolocation.getmCurrentWeatherRequest().loadData(new VolleyCallback() {
#Override
public void onSuccess(CurrentWeatherEntity currentWeatherEntity) {
Log.i(TAG, currentWeatherEntity.getmCity());
}
});
}
}
}
CurrentGeolocation
package com.salvo.weather.android.geolocation;
import android.content.Context;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
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.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.salvo.weather.android.app.VolleyCallback;
import com.salvo.weather.android.client.request.CurrentWeatherRequest;
import com.salvo.weather.android.entity.CurrentGeolocationEntity;
import com.salvo.weather.android.entity.CurrentWeatherEntity;
/**
* Created by mazzy on 30/05/15.
*/
public class CurrentGeolocation
implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener {
private static final String TAG = CurrentGeolocation.class.getSimpleName();
// SETTING CONSTANTS FOR THE LOCATION
private static final long UPDATE_INTERVAL_IN_MILLISECONDS = 10000; //ms
private static final long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS = UPDATE_INTERVAL_IN_MILLISECONDS / 2;
private static CurrentGeolocation sCurrentGeolocation;
private boolean mRequestingLocationUpdates;
private Context mAppContext;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
private CurrentGeolocationEntity mCurrentGeolocationEntity;
private CurrentWeatherRequest mCurrentWeatherRequest;
public static CurrentGeolocation get(Context appContext) {
if (sCurrentGeolocation == null) {
sCurrentGeolocation = new CurrentGeolocation(appContext.getApplicationContext());
}
return sCurrentGeolocation;
}
private CurrentGeolocation(Context appContext) {
mAppContext = appContext;
mRequestingLocationUpdates = true;
mCurrentGeolocationEntity = new CurrentGeolocationEntity();
mCurrentWeatherRequest = CurrentWeatherRequest.get(appContext);
buildGoogleApiClient();
}
#Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "Connected to GoggleApiClient");
if (mCurrentGeolocationEntity.getmLastLocation() == null) {
mCurrentGeolocationEntity.setmLastLocation(LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient));
mCurrentWeatherRequest.setmCurrentGeolocationEntity(mCurrentGeolocationEntity);
}
if (mRequestingLocationUpdates) {
startLocationUpdates();
}
}
#Override
public void onConnectionSuspended(int i) {
// 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();
}
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
// Refer to the javadoc for ConnectionResult to see what error codes might be returned in
// onConnectionFailed.
Log.i(TAG, "Connection failed: error " + connectionResult.getErrorCode());
}
#Override
public void onLocationChanged(Location location) {
// update the location
mCurrentGeolocationEntity.setmLastLocation(location);
}
public GoogleApiClient getmGoogleApiClient() {
return mGoogleApiClient;
}
public void startLocationUpdates() {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
public void stopLocationUpdates() {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
public CurrentWeatherRequest getmCurrentWeatherRequest() {
return mCurrentWeatherRequest;
}
private synchronized void buildGoogleApiClient() {
Log.i(TAG, "Building Google Api Client");
mGoogleApiClient = new GoogleApiClient.Builder(mAppContext)
.addConnectionCallbacks(this)
.addApi(LocationServices.API)
.build();
createLocationRequest();
}
private void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
}
You must create a Listener.
TodayFragment
package com.salvo.weather.android.fragment;
import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.salvo.weather.android.R;
import com.salvo.weather.android.app.VolleyCallback;
import com.salvo.weather.android.entity.CurrentWeatherEntity;
import com.salvo.weather.android.geolocation.CurrentGeolocation;
/**
* A simple {#link Fragment} subclass.
*/
public class TodayFragment extends Fragment implements CurrentGeoloaction.OnUpdateListener {
private static final String TAG = TodayFragment.class.getSimpleName();
private CurrentGeolocation mCurrentGeolocation;
public TodayFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_today, container, false);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mCurrentGeolocation = CurrentGeolocation.get(getActivity());
mCurrentGeolocation.setOnUpdateListener(this);
}
#Override
public void onStart() {
super.onStart();
mCurrentGeolocation.getmGoogleApiClient().connect();
renderView();
}
#Override
public void onPause() {
super.onPause();
if (mCurrentGeolocation.getmGoogleApiClient().isConnected()) {
mCurrentGeolocation.stopLocationUpdates();
}
}
#Override
public void onResume() {
super.onResume();
if (mCurrentGeolocation.getmGoogleApiClient().isConnected()) {
mCurrentGeolocation.startLocationUpdates();
}
}
#Override
public void onStop() {
super.onStop();
if (mCurrentGeolocation.getmGoogleApiClient().isConnected()) {
mCurrentGeolocation.stopLocationUpdates();
}
}
#Override
public void onUpdate() {
renderView();
}
public void renderView() {
// check if googleApiClient is connected
if (mCurrentGeolocation.getmGoogleApiClient().isConnected()) {
mCurrentGeolocation.getmCurrentWeatherRequest().loadData(new VolleyCallback() {
#Override
public void onSuccess(CurrentWeatherEntity currentWeatherEntity) {
Log.i(TAG, currentWeatherEntity.getmCity());
}
});
}
}
}
CurrentGeolocation
package com.salvo.weather.android.geolocation;
import android.content.Context;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
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.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.salvo.weather.android.app.VolleyCallback;
import com.salvo.weather.android.client.request.CurrentWeatherRequest;
import com.salvo.weather.android.entity.CurrentGeolocationEntity;
import com.salvo.weather.android.entity.CurrentWeatherEntity;
/**
* Created by mazzy on 30/05/15.
*/
public class CurrentGeolocation
implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener {
private static final String TAG = CurrentGeolocation.class.getSimpleName();
// SETTING CONSTANTS FOR THE LOCATION
private static final long UPDATE_INTERVAL_IN_MILLISECONDS = 10000; //ms
private static final long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS = UPDATE_INTERVAL_IN_MILLISECONDS / 2;
private static CurrentGeolocation sCurrentGeolocation;
private boolean mRequestingLocationUpdates;
private Context mAppContext;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
private CurrentGeolocationEntity mCurrentGeolocationEntity;
private CurrentWeatherRequest mCurrentWeatherRequest;
private OnUpdateListener mOnUpdateListener;
public interface OnUpdateListener {
public void onUpdate();
}
public void setOnUpdateListener(Fragment todayFragment) {
this.mOnUpdateListener = (OnUpdateListener) todayFragment;
}
public static CurrentGeolocation get(Context appContext) {
if (sCurrentGeolocation == null) {
sCurrentGeolocation = new CurrentGeolocation(appContext.getApplicationContext());
}
return sCurrentGeolocation;
}
private CurrentGeolocation(Context appContext) {
mAppContext = appContext;
mRequestingLocationUpdates = true;
mCurrentGeolocationEntity = new CurrentGeolocationEntity();
mCurrentWeatherRequest = CurrentWeatherRequest.get(appContext);
buildGoogleApiClient();
}
#Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "Connected to GoggleApiClient");
if (mCurrentGeolocationEntity.getmLastLocation() == null) {
mCurrentGeolocationEntity.setmLastLocation(LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient));
mCurrentWeatherRequest.setmCurrentGeolocationEntity(mCurrentGeolocationEntity);
}
if (mRequestingLocationUpdates) {
startLocationUpdates();
mOnUpdateListener.onUpdate();
}
}
#Override
public void onConnectionSuspended(int i) {
// 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();
}
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
// Refer to the javadoc for ConnectionResult to see what error codes might be returned in
// onConnectionFailed.
Log.i(TAG, "Connection failed: error " + connectionResult.getErrorCode());
}
#Override
public void onLocationChanged(Location location) {
// update the location
mCurrentGeolocationEntity.setmLastLocation(location);
}
public GoogleApiClient getmGoogleApiClient() {
return mGoogleApiClient;
}
public void startLocationUpdates() {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
public void stopLocationUpdates() {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
public CurrentWeatherRequest getmCurrentWeatherRequest() {
return mCurrentWeatherRequest;
}
private synchronized void buildGoogleApiClient() {
Log.i(TAG, "Building Google Api Client");
mGoogleApiClient = new GoogleApiClient.Builder(mAppContext)
.addConnectionCallbacks(this)
.addApi(LocationServices.API)
.build();
createLocationRequest();
}
private void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
}
I'm unable to get a Location on my Android Wear Emulator. I've tried many tutorials but non worked and I don't understand whats wrong. Below is my sample source Code which is pretty similar to the original Android Demo from google.
onLocationChanged never gets executed and when I try to retrieve the last Location I alway got NULL
import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.support.wearable.view.WatchViewStub;
import android.view.View;
import android.widget.TextView;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.wearable.Wearable;
public class MainActivity extends Activity implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener {
private TextView mTextView;
private static final long UPDATE_INTERVAL_MS = 2000;
private static final long FASTEST_INTERVAL_MS = 1000;
private GoogleApiClient mGoogleApiClient;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
#Override
public void onLayoutInflated(WatchViewStub stub) {
mTextView = (TextView) stub.findViewById(R.id.text);
}
});
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
System.out.println("Connect to Playservices...");
mGoogleApiClient.connect();
}
#Override
public void onConnected(Bundle bundle) {
System.out.println("onConnected");
LocationRequest locationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(UPDATE_INTERVAL_MS)
.setFastestInterval(FASTEST_INTERVAL_MS);
LocationServices.FusedLocationApi
.requestLocationUpdates(mGoogleApiClient, locationRequest, this)
.setResultCallback(new ResultCallback<Status>() {
#Override
public void onResult(Status status) {
if (status.getStatus().isSuccess()) {
System.out.println("Successfully requested location updates"+status.getStatusMessage());
} else {
System.out.println(
"Failed in requesting location updates, "
+ "status code: "
+ status.getStatusCode() + ", message: " + status
.getStatusMessage());
}
}
});
}
public void getLocation(View view)
{
Location loc=LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if(loc!=null) {
System.out.println("Location: " + loc.getAltitude() + "\n" + loc.getProvider() + "\n" + loc.getLongitude());
}
else
{
System.out.println("Location null");
}
}
#Override
public void onConnectionSuspended(int i) {
System.out.println("onConnectionSuspended(): connection to location client suspended");
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
System.out.println("onConnectionFailed(): connection to location client failed");
}
#Override
public void onLocationChanged (Location location)
{
System.out.println("onLocationchanged: new location"+location.getAltitude());
}
#Override
protected void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
mGoogleApiClient.disconnect();
System.out.println("onstop");
}
#Override
protected void onResume() {
super.onResume();
mGoogleApiClient.connect();
System.out.println("onresume");
}
#Override
protected void onPause() {
super.onPause();
if (mGoogleApiClient.isConnected()) {
LocationServices.FusedLocationApi
.removeLocationUpdates(mGoogleApiClient, this);
}
mGoogleApiClient.disconnect();
System.out.println("onPause");
}
}