在应用启动期间获取当前位置

Get current location during app launch(在应用启动期间获取当前位置)
本文介绍了在应用启动期间获取当前位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天!我正在开发一个监控用户位置的安卓应用程序.我正在使用 LocationManager 获取用户位置,使用以下方法

Good Day! I am working on an android app which monitors user location. I am using LocationManager to get users location, using the following method

public void onLocationChanged(Location theLocation) {}

通过上述方法,每当有用户移动时,我都会收到位置坐标.

Through the above method, whenever there was a user movement, I am receiving location coordinates.

但是,现在我计划在用户登录应用后立即获取用户的位置.有什么方法可以通过 LocationManager 在我的应用启动后手动获取位置坐标?

But, now I am planning to get user's location immediately after their app login. Is there any way through LocationManager through which I can manually get the location coordinates after my app launch?

推荐答案

使用这个技巧:

LocationManager locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

boolean network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

Location location;

if(network_enabled){

   location = locManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

if(location!=null){
   longitude = location.getLongitude();
   latitude = location.getLatitude();
    }                
}

在这种情况下,您甚至不需要使用 GPS,只需移动网络即可.

In this case you even no need to on GPS only your mobile network will do.

别忘了在 Manifest 中给予以下权限:

Don't forget to give the following permission in Manifest:

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

这篇关于在应用启动期间获取当前位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)