广州达内培训

达内首页 > 职业前景 > 使用位置管理器
使用位置管理器
作者:广州达内科技 更新时间:2013-03-27 12:00 来源:Android开发培训

 

Using the Location Manager
只需要进行一些简单的设置,你的应用程序就可以接受位置更新,在这次教程里你将详细的学习这些步骤。
在Manifest里声明合适的权限
要想获取位置更新,第一步需要在manifest里声明合适的权限。如果忘了声明相应的权限,那么你的应用在运行时会报安全异常。当你使用LocationManagement方法的时候,需要设置权限ACCESS_CORASE_LOCATION或者 ACCESS_FINE_LOCATION,例如,如果你的应用使用了基于网络的信息服务,你需要声明N ACCESS_CORASE_LOATION权限,要想获取GPS请求你需要声明ACCESS_FINE_LOCATION权限。值得注意的是如果你声明了ACCESS_FINE_LOCATION权限隐含着你也声明了ACCESS_CORASE_LOCATION权限。 假如一个应用使用了基于网络的位置的信息服务,你需要声明因特网权限。
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
获得一个位置管理的引用
LocationManager是一个主类,在android里你通过这个类你可以使位置服务。使用方法类似于其他的服务,通过调用getSystemService方法可以获得相应的引用。如果你的应用想要在前台(在Activity里)获得位置更新,你应该在onCreate()里执行以下语句。
LocationManager locationManager =        (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
挑选一个位置提供者
当没有请求的时候,现在大部分android电源管理可以通过多种底层技术可以获得位置更新,这种技术被抽象为LocationProvider类的应用。在时间、精度、成本、电源消耗等方面,位置提供者有不同的运行特性。通常,像GPS,一个精确的位置提供者,需要更长的修正时间,而不是不精确,比如基于网络的位置提供者。 通过权衡之后你必须选择一种特殊的位置提供者,或者多重提供者,这些都依赖与你的应用的客户需求。例如,比如说一个关键点的签到服务,需要高精度定位,而一个零售商店定位器使用城市级别的修正就可以满足。下面的代码段要求一个GPS提供者的支持。
LocationProvider provider =        locationManager.getProvider(LocationManager.GPS_PROVIDER);
你提供一些输入标准,比如精度、功率需求、成本等等,让android决定一个最合适的位置匹配提供者。下边的代码片段需要的是更精确的位置提供者而不是考虑成本。需要注意的是这个标准不能帮你解决任何的提供者,可能返回值为空。这个时候你的应用应该能够很好的处理这种情况
// 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.
if (providerName != null) {
   ...
}
检查位置提供者是否使能
在设置里,一些位置提供者比如GPS可以被关闭。良好的做法就是通过调用isProviderEnabled()方法来检测你想要的位置提供者是否打开。如果位置提供者被关闭了,你可以在设置里通过启动Intent来让用户打开。
@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.
    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()
    }
}
private void enableLocationSettings() {
    Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    startActivity(settingsIntent);
}

上一篇:[开源类库/项目] GridView简单应用
下一篇:解说混合式自动重送请求技术

相关资讯

  • [2013-04-02 11:56:00] C# winform Settings 工作原理和方法说明
  • [2013-03-29 11:57:00] 解说混合式自动重送请求技术
  • [2013-03-27 12:00:00] 使用位置管理器
  • [2013-03-25 11:02:50] [开源类库/项目] GridView简单应用
  • [2013-03-22 10:06:26] C# winform Settings 工作原理和方法说明
  • [2013-03-20 10:48:42] 数十倍速下载新时代─解析HSDPA技术原理
  • 【2013年1月18日】
    Java 软件工程师就业班
    >>热招中!


    【2013年1月12日】
    3G-Android 工程师就业班
    >>热招中!


    【2013年1月15日】
    3G-Android 工程师周末班
    >>热招中!

    【2013年1月15日】
    软件测试 工程师就业班
    >>热招中!