Android 在我的中国安卓可穿戴设备中获得GPS定位
我正在为一款智能手表开发一款Android应用程序,这款手表是我买来的,目前正在中国运行 我的目标是获取设备的GPS位置并跟踪其运动,但我似乎无法找到如何使其工作 目前,我正在为API 21开发,我正在使用谷歌Play服务的以下库,因为它是中国可穿戴设备使用的库:Android 在我的中国安卓可穿戴设备中获得GPS定位,android,gps,wear-os,Android,Gps,Wear Os,我正在为一款智能手表开发一款Android应用程序,这款手表是我买来的,目前正在中国运行 我的目标是获取设备的GPS位置并跟踪其运动,但我似乎无法找到如何使其工作 目前,我正在为API 21开发,我正在使用谷歌Play服务的以下库,因为它是中国可穿戴设备使用的库:com.Google.android.gms:Play Services wearable:7.8.87 我使用的代码如下所示: protected void onCreate(Bundle savedInstanceState) {
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();
}
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_on_route);
locationRequest=locationRequest.create()
.setPriority(定位请求。优先级高精度)
.setInterval(更新间隔时间)
.SetFastTestInterval(最快间隔);
mgoogleapclient=新的Googleapclient.Builder(此)
.addApi(LocationServices.API)
.addApi(Wearable.API)//用于数据层API
.addConnectionCallbacks(此)
.addOnConnectionFailedListener(此)
.build();
startTravelTime=System.currentTimeMillis();
如果(!hasGps()){
Log.d(标记“此硬件没有GPS”);
}
}
私有布尔hasGps(){
返回getPackageManager().hasSystemFeature(PackageManager.FEATURE\u LOCATION\u GPS);
}
@凌驾
受保护的void onStart(){
super.onStart();
mGoogleApiClient.connect();
Log.i(“已连接!!!”,“已连接”);
}
@凌驾
受保护的void onStop(){
super.onStop();
if(mgoogleapClient.isConnected()){
LocationServices.FusedLocationApi.RemovelocationUpdate(mgoogleapClient,this);
}
mGoogleApiClient.disconnect();
System.out.println(“onstop”);
}
@凌驾
受保护的void onResume(){
super.onResume();
如果(!mgoogleapClient.isConnected()){
mGoogleApiClient.connect();
}
}
@凌驾
受保护的void onPause(){
super.onPause();
if(mgoogleapClient.isConnected()){
LocationServices.FusedLocationApi.RemovelocationUpdate(mgoogleapClient、OnRouteActivity.this);
}
mGoogleApiClient.disconnect();
}
@凌驾
未连接的公共空间(捆绑包){
LocationServices.FusedLocationApi.requestLocationUpdates(mgoogleAppClient,locationRequest,this).setResultCallback(new ResultCallback()){
@凌驾
公共void onResult(状态){
if(status.getStatus().issucess()){
if(Log.isLoggable(标记,Log.DEBUG)){
Log.d(标记“成功请求的位置更新”);
}
}否则{
Log.e(标签,
请求位置更新失败
+“状态代码:”
+status.getStatusCode()
+,消息:
+status.getStatusMessage());
}
}
});
}
@凌驾
已更改位置上的公共无效(位置){
//在UI中显示纬度和经度
mTextView=(TextView)findViewById(R.id.location);
mTextView.setText(“纬度:+String.valueOf(location.getLatitude())+”\n纬度:+String.valueOf(location.getLongitude());
Toast.makeText(getBaseContext(),“Latitude:”+String.valueOf(location.getLatitude())+“\n长度:”+String.valueOf(location.getLatitude()),Toast.LENGTH_LONG.show();
}
我希望有人能帮助我:)要在中国可穿戴设备上工作,您应该使用,并确保在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'
}
更多详细信息可在谷歌指南中找到问题是,当我使用该版本的谷歌游戏服务时,会出现以下错误:谷歌游戏服务过时。需要10298000,但找到7571430你知道有哪个更低版本可以与我的手表配合使用吗?我正在使用emulator(Android Studio 3.0的金丝雀9版本的一部分)来测试我的应用程序。到目前为止,我还没有遇到任何问题。我不知道您是否可以使用较低版本,但根据文档,它听起来不像。也许你的手表太旧了,不受支持?我考虑过这种可能性:(我将用模拟器测试我的代码,看看它是否在那里工作,如果是,问题肯定是手表的版本。谢谢!