在我的Android可穿戴设备中获取GPS位置
问题描述:
我正在为购买并在中国运行的智能手表开发Android应用程序。在我的Android可穿戴设备中获取GPS位置
我的目标是获取设备的GPS位置并跟踪其移动,但我似乎无法找到如何使其工作。
目前,我正在开发API 21和我使用谷歌的下库播放服务,因为它是在中国使用的穿戴式的一个:com.google.android.gms:play-services-wearable:7.8.87
,我使用的代码是以下:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_on_route);
locationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(UPDATE_INTERVAL_MS)
.setFastestInterval(FASTEST_INTERVAL_MS);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addApi(Wearable.API) // used for data layer API
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
startTravelTime = System.currentTimeMillis();
if (!hasGps()) {
Log.d(TAG, "This hardware doesn't have GPS.");
}
}
private boolean hasGps() {
return getPackageManager().hasSystemFeature(PackageManager.FEATURE_LOCATION_GPS);
}
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
Log.i("Connected!!!", "WERE CONNECTED");
}
@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();
if(!mGoogleApiClient.isConnected()) {
mGoogleApiClient.connect();
}
}
@Override
protected void onPause() {
super.onPause();
if (mGoogleApiClient.isConnected()) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, OnRouteActivity.this);
}
mGoogleApiClient.disconnect();
}
@Override
public void onConnected(Bundle bundle) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this).setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
if (status.getStatus().isSuccess()) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Successfully requested location updates");
}
} else {
Log.e(TAG,
"Failed in requesting location updates, "
+ "status code: "
+ status.getStatusCode()
+ ", message: "
+ status.getStatusMessage());
}
}
});
}
@Override
public void onLocationChanged(Location location){
// Display the latitude and longitude in the UI
mTextView = (TextView) findViewById(R.id.location);
mTextView.setText("Latitude: " + String.valueOf(location.getLatitude()) + "\nLongitude: " + String.valueOf(location.getLongitude()));
Toast.makeText(getBaseContext(), "Latitude: " + String.valueOf(location.getLatitude()) + "\nLongitude: " + String.valueOf(location.getLongitude()), Toast.LENGTH_LONG).show();
}
我希望有人能帮助我解决这个:)
答
对于位置对中国的可穿戴式设备工作,你应该使用Fused Location Provider API,并确保在您的build.gradle文件中的以下行:
dependencies {
...
compile 'com.google.android.gms:play-services-location:10.2.0'
}
除了上面你还需要添加:
dependencies {
...
compile 'com.google.android.gms:play-services-wearable:10.2.0'
}
更多细节可以在谷歌的指导Creating Android Wear Apps for China
的问题是找到当我使用的是谷歌的Play版本服务我出现以下错误:_Google Play服务已过期。需要10298000,但找到7571430_ 您是否知道可以与我的手表配合使用的更低版本? – cabanapi
我正在使用模拟器(适用于Android Studio 3.0的Canary 9版本的一部分)来测试我的应用程序。到目前为止,我没有任何问题。我不知道你是否可以使用较低版本,但基于文档听起来不像。也许你的手表被认为太旧了,不支持? – TofferJ
我想过这种可能性:(我会用模拟器测试我的代码,看看它是否在那里工作,如果是这样,问题肯定是手表的版本,谢谢! – cabanapi