首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android 7服务gpsupdate

Android 7服务gpsupdate
EN

Stack Overflow用户
提问于 2019-12-16 19:07:37
回答 1查看 47关注 0票数 1

我有一个用于android7的java服务类,它应该提交设备的GPS位置。见下文。

我遇到的问题是Android Studio抛出了一个异常,比如“任务还没有完成”。

我还尝试将其作为单独的线程运行。

如何解决这个问题?

代码语言:javascript
复制
protected void getLocation() {
    // Create the location request to start receiving updates
    mLocationRequest = new LocationRequest();
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setInterval(UPDATE_INTERVAL);
    mLocationRequest.setFastestInterval(FASTEST_INTERVAL);

    // Create LocationSettingsRequest object using location request
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
    builder.addLocationRequest(mLocationRequest);
    LocationSettingsRequest locationSettingsRequest = builder.build();

    // Check whether location settings are satisfied
    // https://developers.google.com/android/reference/com/google/android/gms/location/SettingsClient
    SettingsClient settingsClient = LocationServices.getSettingsClient(this);
    settingsClient.checkLocationSettings(locationSettingsRequest);

    // new Google API SDK v11 uses getFusedLocationProviderClient(this)
    fusedLocationClient = getFusedLocationProviderClient(this);

    // Approach 1
    setGpsLocation(fusedLocationClient);

    // Approach 2
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                setGpsLocation(fusedLocationClient);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }).start();

}

代码语言:javascript
复制
private void setGpsLocation(FusedLocationProviderClient f) {
    Location location = f.getLastLocation().getResult();

    StringBuilder sb = new StringBuilder();
    sb.append("<Data>");
    sb.append(" <UserId>" + userId + "</UserId>");
    sb.append(" <Type>GpsLocation</Type>");
    sb.append(" <Longitude>" + location.getLongitude() + "</Longitude>");
    sb.append(" <Latitude>" + location.getLatitude() + "</Latitude>");
    sb.append(" <DateTime>" + sdf.format(new Date()) + "</DateTime>");
    sb.append("</Data>");

}

}

EN

回答 1

Stack Overflow用户

发布于 2019-12-16 19:36:21

getLastLocation()方法返回一个任务对象。Task对象表示异步操作(通常是获取某个值的操作,在本例中是Location对象)。您可以从location对象中检索位置的纬度和经度。在极少数情况下,当位置不可用时,location对象为null。

Task类提供了用于添加成功和失败侦听器的方法。如果操作成功,成功的监听器将传递所需的对象。如果操作不成功,则失败监听器将传递异常。

代码语言:javascript
复制
mFusedLocationClient.getLastLocation().addOnSuccessListener(
       new OnSuccessListener<Location>() {
           @Override
           public void onSuccess(Location location) {
               if (location != null) {
                   mLastLocation = location;
               } 
           }
       });

mFusedLocationClient.getLastLocation().addOnFailureListener(
       new OnFailureListener() {
           @Override
           public void onFailure(@NonNull Exception e) {
               Log.e(TAG, "onFailure: ", e.printStackTrace());

           }
       }
);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59355410

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档