博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Using the Location Manager
阅读量:4046 次
发布时间:2019-05-24

本文共 4257 字,大约阅读时间需要 14 分钟。

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

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 at runtime.

Depending on the methods used, either or permission is needed. For example, you need to declare the permission if your application uses a network-based location provider only. The more accurate GPS requires the permission. Note that declaring the permission implies already.

Also, if a network-based location provider is used in the application, you'll need to declare the internet permission as well.

Get a Reference to 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 method. If your application intends to receive location updates in the foreground (within an ), you should usually perform this step in the method.

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 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.

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.

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.

// Retrieve a list of location providers that have fine accuracy, no monetary cost, etcCriteria 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) {   ...}

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 method. If the location provider is disabled, you can offer the user an opportunity to enable it in Settings by firing an with the action.

@Overrideprotected 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://txgdi.baihongyu.com/

你可能感兴趣的文章
测试必会之 Linux 三剑客之 sed
查看>>
Socket请求XML客户端程序
查看>>
Java中数字转大写货币(支持到千亿)
查看>>
Java.nio
查看>>
函数模版类模版和偏特化泛化的总结
查看>>
VMware Workstation Pro虚拟机不可用解决方法
查看>>
最简单的使用redis自带程序实现c程序远程访问redis服务
查看>>
redis学习总结-- 内部数据 字符串 链表 字典 跳跃表
查看>>
iOS 对象序列化与反序列化
查看>>
iOS 序列化与反序列化(runtime) 01
查看>>
iOS AFN 3.0版本前后区别 01
查看>>
iOS ASI和AFN有什么区别
查看>>
iOS QQ侧滑菜单(高仿)
查看>>
iOS 扫一扫功能开发
查看>>
iOS app之间的跳转以及传参数
查看>>
iOS __block和__weak的区别
查看>>
Android(三)数据存储之XML解析技术
查看>>
Spring JTA应用之JOTM配置
查看>>
spring JdbcTemplate 的若干问题
查看>>
Servlet和JSP的线程安全问题
查看>>