【问题标题】:I want to check User's Location Periodically at an interval of 20 Sec. automatically我想每隔 20 秒定期检查用户的位置。自动地
【发布时间】:2016-03-31 11:52:07
【问题描述】:

您好,我正在创建一个 Android 应用程序。其中我包括一个导航模块。 在这里,我想以 20 秒的间隔自动检查用户的位置,我不想放置任何按钮来触发此位置更改事件。这种方法应该像实时谷歌地图导航系统一样自动运行。我正在使用FusedLocationAPILocationListener 获取用户的位置。 我已经创建了这个方法。

    private void onLocationUpdate() {
    lr.setInterval(20000).setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, lr, (com.google.android.gms.location.LocationListener) this);

}

这里lrLocationRequest 的对象。 请告诉我应该在哪里调用此方法。 在onLocationChanged(Location location) 下,我在用户的当前位置添加标记。

【问题讨论】:

    标签: android google-maps navigation


    【解决方案1】:

    你需要创建一个服务:

    public class LocationService extends Service
            implements LocationListener,
            GoogleApiClient.OnConnectionFailedListener,
            GoogleApiClient.ConnectionCallbacks {}
    

    并实现所需的方法。你会得到:

     @Override
        public void onLocationChanged(Location location) {
            //do your stuff
        }
    

    现在由您来检查 X 次是否通过,以及您是否需要对位置进行处理。也不要忘记添加

        compile 'com.google.android.gms:play-services-location:8.+'
        compile 'com.google.android.gms:play-services-maps:8.+' 
    

    在你的毕业典礼上。在你的清单中添加,当然是权限;

     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    

    和您的服务:

    <service
            android:name="{package}.LocationService"
            android:exported="false" />
    

    【讨论】:

      猜你喜欢
      • 2014-11-24
      • 1970-01-01
      • 1970-01-01
      • 2016-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-07
      • 2022-12-20
      相关资源
      最近更新 更多