2

I am making an Android Wear Watch Face and I need to show user current location.

I was followed tutorial on Dev site, but I can't get it to work. The code never goes to "onLocationChanged" listener.

Device is Huawei watch, and it is connected to mobile with WiFi and Location on.

Permissions are on (I have runtime permission request). Here is the code:

AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>

build.gradle (Module.wear)

compile 'com.google.android.gms:play-services-location:8.4.0'

AnalogFaceService.java

googleApiClient = new GoogleApiClient.Builder(AnalogFaceService.this)
  .addApi(LocationServices.API)
  .addApi(Wearable.API)
  .addConnectionCallbacks(this)
  .addOnConnectionFailedListener(this).build();

@Override
public void onConnected(@Nullable Bundle bundle)
{
  LocationRequest locationRequest = LocationRequest.create()
   .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY)
   .setInterval(TimeUnit.SECONDS.toMillis(2))
   .setFastestInterval(TimeUnit.SECONDS.toMillis(2))
   .setSmallestDisplacement(2);

  if
  (
    (
      ActivityCompat.checkSelfPermission(AnalogFaceService.this, Manifest.permission.ACCESS_FINE_LOCATION) 
      != PackageManager.PERMISSION_GRANTED
     ) && (
      ActivityCompat.checkSelfPermission(AnalogFaceService.this, Manifest.permission.ACCESS_COARSE_LOCATION) 
      != PackageManager.PERMISSION_GRANTED
     )
    ) {
      return;
    }

  LocationServices.FusedLocationApi
   .requestLocationUpdates(googleApiClient, locationRequest, onLocationChangedListener)
   .setResultCallback(locationCallback);
}

private final LocationListener onLocationChangedListener = new LocationListener()
{
  @Override
  public void onLocationChanged(Location location)
  {
    Log.v("LOCATION_TEST", location.getLatitude() + " :::: " + location.getLongitude());
    getSunriseAndSunset(location);

    addLocationEntry(location.getLatitude(), location.getLongitude());
  }
};
Reporter
  • 3,897
  • 5
  • 33
  • 47
filipst
  • 1,547
  • 1
  • 30
  • 55
  • Can you post all the code from your activity? – Sid Nov 21 '16 at 14:28
  • 2
    After hours of checking, rechecking and solution searching, I tried to open Google Maps on the watch to see if location is working at all. And when it started, my app started working too. After factory reset, I didn't open Google Maps at all. It is a bit awkward but I guess it works. I will try to revert it to factory settings again and seee what happens. – filipst Nov 22 '16 at 07:42

2 Answers2

1

The code from Dev site was not a problem.

Device was fresh factory reset and I didn't open Google Maps yet so the Watch couldn't get current location and because of that it didn't go into "onLocationChanged".

filipst
  • 1,547
  • 1
  • 30
  • 55
0

You have to call connect.

@Override
protected void onResume() {
    super.onResume();
    googleApiClient.connect();
    ...
}
kazhik
  • 556
  • 1
  • 5
  • 7