問題描述
我正在使用簡單的 位置管理器 對象來獲取設(shè)備的 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
}
已授予權(quán)限:
<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" />
所以有什么需要設(shè)置的嗎?我已檢查我的位置服務(wù)是否已在設(shè)備中打開請?zhí)峁┮恍┕ぷ魇纠逆溄?em class="showen">
so is anything missing to set up? I have checked my location service is on in device please give some links for working examples
推薦答案
這個問題的答案很愚蠢...我重新啟動了我的設(shè)備...它工作了...請確保您的位置服務(wù)已啟用并正常工作正確的
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
這篇關(guān)于如何在 Android 中獲取位置管理器的最后一個已知位置?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!