Android培训翻译_使用定位管理器

 

本文是Android开发官网 Android培训的中文翻译,限于译者水平有限,便于大家指正,采用中英文对照风格

更多已翻译内容请见 Android培训翻译_目录

 

最近店里的事情比较多,又是一年交租期,还要忙去进货,手头上有10来篇草译没来得及核对,等过两天比较清闲了再审完一并发上来。

一天没上已见目录有100多人阅读,感谢大家的关注,也希望自己能坚持下去。


 

这节课教你

  1. Declare Proper Permissions in Android Manifest
    在Android Manifest中声明适当的权限
  2. Get a Reference to LocationManager
    获得一个定位管理器的引用
  3. Pick a Location Provider
    选择一个定位提供器
  4. Verify the Location Provider is Enabled
    验证定位提供器已启用

你该看看

下载示例代码(LocationAware.zip)


 

Before your application can begin receiving location updates, it needs to perform some simple steps to set up access. In this lesson, you’ll learn what these steps entail.
在你的程序可以获取位置更新前,需要执行一些简单的访问步骤。这节课中,你将学到这些步骤的必要性。

 

Declare Proper Permissions in Android Manifest
在Android Manifest中声明适当的权限

The first step of setting up location update access is to declare proper permissions in the manifest. If permissions are missing, the application will get a SecurityException at runtime. 设置位置更新访问的第一步是在manifest中声明合适的权限。如果权限丢失,程序会在运行时得到一个 SecurityException 异常。

 

Depending on the LocationManager methods used, either ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission is needed. For example, you need to declare the ACCESS_COARSE_LOCATION permission if your application uses a network-based location provider only. The more accurate GPS requires the ACCESS_FINE_LOCATION permission. Note that declaring the ACCESS_FINE_LOCATION permission implies ACCESS_COARSE_LOCATION already.
根据 LocationManager 使用的方法, ACCESS_COARSE_LOCATION和ACCESS_FINE_LOCATION 权限都是必须的。例如,如果你的应用程序使用基于网络的定位提供器时,你需要声明 ACCESS_COARSE_LOCATION权限。更精确的GPS需要 ACCESS_FINE_LOCATION 权限。注意,声明 ACCESS_FINE_LOCATION 权限时意味着已经声明 ACCESS_COARSE_LOCATION 权限。

 

Also, if a network-based location provider is used in the application, you’ll need to declare the internet permission as well.
如果基于网络的定位提供器已经在程序中使用,你也要声明因特网权限。

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

 

Get a Reference to LocationManager
获得一个定位管理器的引用

LocationManager is the main class through which your application can access location services on Android. Similar to other system services, a reference can be obtained from calling the getSystemService() method. If your application intends to receive location updates in the foreground (within an Activity), you should usually perform this step in the onCreate() method.
定位管理器是你的程序可以访问Android定位服务的主要类。与其它系统服务相似,可以从调用 getSystemService() 方法获得一个引用。如果你的应用程序想要在前台收到位置更新(使用Activity),你应当常执行 onCreate() 方法中的这步。

 

LocationManager locationManager =
        (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

 

Pick a Location Provider
选择一个定位提供器

While not required, most modern Android-powered devices can receive location updates through multiple underlying technologies, which are abstracted to an application as LocationProvider objects. Location providers may have different performance characteristics in terms of time-to-fix, accuracy, monetary cost, power consumption, and so on. Generally, a location provider with a greater accuracy, like the GPS, requires a longer fix time than a less accurate one, such as a network-based location provider.
尽管并非必须,大多数现代Android支持的设备可以获得位置更新通过多种底层技术,被应用程序抽象为 LocationProvider (定位提供器)对象。定位提供器可能有不同的性能特征,校准时间,精确度,货币成本,功耗等。通常一个像GPS这样高精准的定位提供器,需要比基于网络的这种较低精确度的定位提供器更长的校准时间。

 

Depending on your application’s use case, you have to choose a specific location provider, or multiple providers, based on similar tradeoffs. For example, a points of interest check-in application would require higher location accuracy than say, a retail store locator where a city level location fix would suffice. The snippet below asks for a provider backed by the GPS.
根据你应用程序的用例,你必须选择一个定位提供器,或者多个提供器,基于类似的权衡。例如,一个兴趣点检查应用程序可能需要更高定位精准度,一个城市级别的位置校准定位器就可以满足零售商店。下面代码段请求一个提供器,返回GPS。

LocationProvider provider =
        locationManager.getProvider(LocationManager.GPS_PROVIDER);

 

Alternatively, you can provide some input criteria such as accuracy, power requirement, monetary cost, and so on, and let Android decide a closest match location provider. The snippet below asks for a location provider with fine accuracy and no monetary cost. Note that the criteria may not resolve to any providers, in which case a null will be returned. Your application should be prepared to gracefully handle the situation.
另外,你可以提供一些输入条件,例如精度,能耗,货币成本等,然后让Android决定最符合的定位提供器。下面的代码段用良好精度和无货币成本请求一个定位提供器。注意,条件可能不能符合任何提供器,这种情况将会返回null。你的程序应该能够优雅的处理这种情况。

// Retrieve a list of location providers that have fine accuracy, no monetary cost, etc 
// 检索一个良好精度,无货币成本的定位提供器

Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setCostAllowed(false);
...
String providerName = locManager.getBestProvider(criteria, true);

// If no suitable provider is found, null is returned. 
// 没有合适的提供器,则返回null
if (providerName != null) {
   ...
}

 

Verify the Location Provider is Enabled
验证定位提供器已启用

Some location providers such as the GPS can be disabled in Settings. It is good practice to check whether the desired location provider is currently enabled by calling the isProviderEnabled() method. If the location provider is disabled, you can offer the user an opportunity to enable it in Settings by firing an Intent with the ACTION_LOCATION_SOURCE_SETTINGS action.
一些定位提供器可以在设置中被禁用。通过调用 isProviderEnable()方法来确认定位提供器是否已启用是个很好的实践。如果定位管理器被禁用,你可以通过使用 ACTION_LOCATION_SOURCE_SETTING 动作发送一个意图,给用户一个在设置中启用它的机会。

 

@Override
protected void onStart() {
    super.onStart();

    // This verification should be done during onStart() because the system calls
    // this method when the user returns to the activity, which ensures the desired
    // location provider is enabled each time the activity resumes from the stopped state.
    // 因为系统在用户返回这个Activity时调用这个方法,所以在onStart()期间,验证应该已完成。Activity每次从停止状态恢复时都应当确认定位提供器是否已启用。 
    LocationManager locationManager =
            (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    final boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

    if (!gpsEnabled) {
        // Build an alert dialog here that requests that the user enable
        // the location services, then when the user clicks the "OK" button,
        // call enableLocationSettings()
        // 构建一个警示对话框,要求用户启用定位服务,当用户点击“OK”按钮时调用enableLocationSettings() 
    }
}

private void enableLocationSettings() {
    Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    startActivity(settingsIntent);
}

转载于:https://www.cnblogs.com/stonesea/archive/2012/08/23/using-the-location-manager.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值