Android Using Location Manager

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权限。
假如一个应用使用了基于网络的位置的信息服务,你需要声明因特网权限。

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

获得一个位置管理的引用


LocationManager是一个主类,在android里你通过这个类你可以使位置服务。使用方法类似于其他的服务,通过调用getSystemService方法可以获得相应的引用。如果你的应用想要在前台(在Activity里)获得位置更新,你应该在onCreate()里执行以下语句。

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

挑选一个位置提供者


当没有请求的时候,现在大部分android电源管理可以通过多种底层技术可以获得位置更新,这种技术被抽象为LocationProvider类的应用。在时间、精度、成本、电源消耗等方面,位置提供者有不同的运行特性。通常,像GPS,一个精确的位置提供者,需要更长的修正时间,而不是不精确,比如基于网络的位置提供者。
通过权衡之后你必须选择一种特殊的位置提供者,或者多重提供者,这些都依赖与你的应用的客户需求。例如,比如说一个关键点的签到服务,需要高精度定位,而一个零售商店定位器使用城市级别的修正就可以满足。下面的代码段要求一个GPS提供者的支持。

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

你提供一些输入标准,比如精度、功率需求、成本等等,让android决定一个最合适的位置匹配提供者。下边的代码片段需要的是更精确的位置提供者而不是考虑成本。需要注意的是这个标准不能帮你解决任何的提供者,可能返回值为空。这个时候你的应用应该能够很好的处理这种情况

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// 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来让用户打开。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
@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);
}
转自:http://wiki.eoe.cn/page/Using_the_Location_Manager.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值