我正在製作Android Wear手錶Face,我需要顯示用戶當前的位置。Android Wear位置
我被跟在Dev site上的教程,但我不能得到它的工作。代碼永遠不會去「onLocationChanged」偵聽器。
設備是華爲手錶,它通過WiFi和位置連接到手機。
權限在(我有運行時權限請求)。下面是代碼:
的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());
}
};
你可以發佈你的活動的所有代碼嗎? – Sid
經過幾個小時的檢查,重新檢查和解決方案搜索之後,我嘗試在手錶上打開Google地圖,查看位置是否正常工作。 當它開始時,我的應用程序也開始工作。 出廠重置後,我根本沒有打開Google地圖。這有點尷尬,但我想它的工作原理。我會嘗試將其恢復到出廠設置,並會看到會發生什麼。 – filipst