应用程序启动过程中获取的当前位置 [英] Get current location during app launch

查看:133
本文介绍了应用程序启动过程中获取的当前位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天! 我工作的一个Android应用程序,它监视用户的位置。我使用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.

不要忘了给下面的许可清单:

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"/>

这篇关于应用程序启动过程中获取的当前位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆