問題描述
美好的一天!我正在開發(fā)一個(gè)監(jiān)控用戶位置的安卓應(yīng)用程序.我正在使用 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) {}
通過上述方法,每當(dāng)有用戶移動(dòng)時(shí),我都會(huì)收到位置坐標(biāo).
Through the above method, whenever there was a user movement, I am receiving location coordinates.
但是,現(xiàn)在我計(jì)劃在用戶登錄應(yīng)用后立即獲取用戶的位置.有什么方法可以通過 LocationManager 在我的應(yīng)用啟動(dòng)后手動(dòng)獲取位置坐標(biāo)?
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?
推薦答案
使用這個(gè)技巧:
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,只需移動(dòng)網(wǎng)絡(luò)即可.
In this case you even no need to on GPS only your mobile network will do.
別忘了在 Manifest 中給予以下權(quán)限:
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"/>
這篇關(guān)于在應(yīng)用啟動(dòng)期間獲取當(dāng)前位置的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!