Android Wear get GPS Coordinates - android

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");
}
}

Related

Retrieving current location in android of API Level 19 to 22

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);
}
}
}
}

Location only updating TextView when app is out of focus

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.

Geo Location Co-ordinates using GooglePlayService

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());
}
}

onLocationChanged is not call continuously?

I wrote some code to get a continuously track location it works but the update triggers only sometimes after restarting the app after the screen timeout, but not allways...
In the "Google Play Services", i use only com.google.android.gms:play-services-location:8.3.0 (to reduce the size).
The AndroidManifest.xml is set for an ACCESS_FINE_LOCATION" permission.
The GPS and "Wi-Fi & mobile network location" of my device is on.
Thanks in advance for help.
Here is the main part:
import android.location.Location;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import comm.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;
public class MainActivity extends AppCompatActivity implements
ConnectionCallbacks, OnConnectionFailedListener, LocationListener {
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
protected TextView txtlat, txtlong;
// ---------------------------------------------------------------------------------------
// Create
// ---------------------------------------------------------------------------------------
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtlat = (TextView)findViewById(R.id.txtlat);
txtlong = (TextView)findViewById(R.id.txtlong);
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
createLocationRequest();
}
protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000); // 10sec
mLocationRequest.setFastestInterval(5000); // 5sec
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
// ----------------------------------------------------------------------
#Override
protected void onResume() {
super.onResume();
mGoogleApiClient.connect();
}
// ---------------------------------------------------------------------
#Override
protected void onPause() {
super.onPause();
if (mGoogleApiClient.isConnected()) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
mGoogleApiClient.disconnect();
}
}
// ---------------------------------------------------------------------
#Override
public void onConnected(Bundle connectionHint) {
Location location=LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (location == null) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
} else {
displayLocation(location);
}
}
// ---------------------------------------------------------------------
public void onLocationChanged (Location location) {
displayLocation(location);
}
// ---------------------------------------------------------------------
#Override
protected void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
}

How to update a fragment from another class that isn't an fragment

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);
}
}

Categories

Resources