本文介紹了如何在 Android 中獲取位置管理器的最后一個已知位置?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我正在使用簡單的 位置管理器 對象來獲取設備的 lastKnownLocation()但是在 return 中得到 null object 誰能告訴我為什么?
I am using simple location manager object to get lastKnownLocation() of device but getting null object in return can any one tell me why ?
代碼:
public Location getLocation() {
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
if (locationManager != null) {
Location lastKnownLocationGPS = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (lastKnownLocationGPS != null) {
return lastKnownLocationGPS;
} else {
Location loc = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
System.out.println("1::"+loc);----getting null over here
System.out.println("2::"+loc.getLatitude());
return loc;
}
} else {
return null;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.location);
getLocation();-----calling service
}
已授予權限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
所以有什么需要設置的嗎?我已檢查我的位置服務是否已在設備中打開請提供一些工作示例的鏈接
so is anything missing to set up? I have checked my location service is on in device please give some links for working examples
推薦答案
這個問題的答案很愚蠢...我重新啟動了我的設備...它工作了...請確保您的位置服務已啟用并正常工作正確的
Silly thing as answer to this ...I restarted my device ...and it worked...Please ensure that your service for location are enabled and working properly
這篇關于如何在 Android 中獲取位置管理器的最后一個已知位置?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!