【问题标题】:Get GPS navigation location in a fixed interval of time, android?在固定的时间间隔内获取GPS导航位置,android?
【发布时间】:2021-11-15 03:34:38
【问题描述】:

好的,我的应用程序是你可以说的 GPS 跟踪器应用程序。

如果用户从源头到目的地旅行,我想在 5-7 分钟后获取其 GPS 位置,直到用户到达目的地,并继续向指定号码发送该位置的短信。

所以我的问题是如何在用户关闭应用程序后获取用户的位置并发送消息?

用户将在我的活动中填写源和目标,然后单击按钮发送文本消息并关闭应用程序。

我想这可以通过使用android的服务类来解决......但我真的不明白我该如何使用它?

【问题讨论】:

标签: android service navigation gps


【解决方案1】:

使用LocationManager 类的requestLocationUpdates() 在特定时间间隔内获取GPS 位置。查看this 供您参考。

请参阅以下代码 sn-p ,它将每 1 分钟获取用户当前的经纬度位置。

public Location getLocation() {
    Location location = null;
    try {
        LocationManager locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE); 
        // Getting GPS status
        boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        // If GPS Enabled get lat/long using GPS Services

        if (isGPSEnabled) {
            if (location == null) {
                locationManager.requestLocationUpdates( 
                    LocationManager.GPS_PROVIDER, 
                    MIN_TIME_BW_UPDATES, 
                    MIN_DISTANCE_CHANGE_FOR_UPDATES,
                    this
                );

                if (locationManager != null) {
                    location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude(); 
                        longitude = location.getLongitude(); 
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return location;
} 

得到Latitude和Longitude后使用Geocoder获取详细地址。

public List<Address> getCurrentAddress() {
    List<Address> addresses = new ArrayList<Address>();
    Geocoder gcd = new Geocoder(mContext,Locale.getDefault());
    try {
        addresses = gcd.getFromLocation(latitude, longitude, 1);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return addresses;
}

编辑:

在extends Service 的类中执行所有上述功能,以便即使应用程序关闭,它也可以跟踪用户位置。请参阅下面的示例

public final class LocationTracker extends Service implements LocationListener {
    // Your code here
}

【讨论】:

  • 如果用户已关闭应用程序,我如何获取位置??
  • 使用Service 来实现这一点,即使应用程序关闭,它也会在后台运行。查看我编辑的答案以使用Service
  • 我应该使用什么条件或循环来获取位置并在每 5 分钟后发送短信???
猜你喜欢
  • 1970-01-01
  • 2013-03-25
  • 2017-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-10
相关资源
最近更新 更多