android - 在中国的 Android 可穿戴设备中获取 GPS 位置

标签 android gps wear-os

我正在为在中国购买并运行的智能 watch 开发 Android 应用。

我的目标是获取设备的 GPS 位置并跟踪其移动,但我似乎找不到如何让它工作。

目前,我正在为 API 21 开发,我正在使用以下 Google Play 服务库,因为它是中国可穿戴设备使用的库:com.google.android.gms:play-services -可穿戴: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'
}

可以在 Google 指南中找到更多详细信息 Creating Android Wear Apps for China

关于android - 在中国的 Android 可穿戴设备中获取 GPS 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45631330/

相关文章:

java - 在android中的 View 上使用append方法出现NullPointerException?

Android - Java 泛型 SpinnerAdapter

java - 使用 Assets 时 Android WebView 内存泄漏

android - 服务测试用例中setTestProviderLocation不触发onLocationChanged

android - 在android中使用纬度和经度值获取位置名称

android - requestLocationUpdates()参数改变Gingerbread到ICS的效果?

android - ListView 在加载更多时重复数据

android - 是否可以自定义 android wear SpeechRecognizer UI?

android - 这个 Android Wear 应用程序在哪里?

java - 使用不止 setStatusBarGravity 在表盘上移动状态栏