![](/img/trans.png)
[英]android getLastKnownLocation() return always return null
[英]Why the function getLastKnownLocation return Null on Android SDK?
我在eclipse上使用android sdk,我想從設備獲取位置,所以我在其他類中編寫了一個程序(與main不同),並從主類中調用mContext函數。
package com.example.deomanapp;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import com.example.deomanapp.MainActivity.PlaceholderFragment;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class Mhelper {
public void mContext(Context context)
{
LocationManager lm;
lm = (LocationManager)context.getSystemService(context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
String slongitude = String.valueOf((long) longitude);
String slatitude = String.valueOf((long) latitude);
Toast.makeText(context, slongitude, Toast.LENGTH_SHORT).show();
}
}
問題是getLongitude或getLatitude返回null,因此該程序在此行與此日志崩潰:
04-20 04:30:30.410: E/AndroidRuntime(5151): java.lang.NullPointerException
04-20 04:30:30.410: E/AndroidRuntime(5151): at com.example.deomanapp.Mhelper.mContext(Mhelper.java:29)
此代碼有什么問題?
PS:我讀了其他標題相同且主題沒有幫助的問題(它們都不具有實際答案),因為:
1-我在啟用GPS且可以正常工作的真實設備(非仿真器)上測試了該程序,但是盡管Device之前已獲得其位置,但該程序無法獲取位置,並且必須顯示LastKnownLocation 。
2-我給程序ACCESS_FINE_LOCATION和ACCESS_COARSE_LOCATION權限。
3-我什至使用了一項功能來查看GPS是否打開,當我將其關閉時,程序會提醒我。
問這是一個愚蠢的問題,因為它為NULL,所以為null,我通過對主要問題的評論來理解它,我的解決方案是在sdk管理器中下載android api圖像 ,運行一次Google地圖 ,然后通過DDMS將GPS位置發送給它運行程序。
我試圖修改一些代碼,希望它能滿足您的需求。
您將在答案的底部找到我的LocationsCoreModule代碼的實現:
LocationsCoreModule locationService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LocationRequest mLocationRequest = LocationRequest.create();
// Use high accuracy
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
// Set the update interval to 5 seconds
mLocationRequest.setInterval(5000);
// Set the fastest update interval to 1 second
mLocationRequest.setFastestInterval(1000);
locationService = new LocationsCoreModule(this, mLocationRequest);
locationService.setLocationsListener(new LocationsCoreModuleCallback() {
@Override
public void locationClientConnected() {
Location location = locationService.getLastLocation();
double longitude = location.getLongitude();
double latitude = location.getLatitude();
String slongitude = String.valueOf((long) longitude);
String slatitude = String.valueOf((long) latitude);
Toast.makeText(getApplicationContext(), slongitude, Toast.LENGTH_SHORT).show();
}
});
}
如果您希望應用程序立即開始偵聽新的GPS位置:
@Override
protected void onStart() {
super.onStart();
locationService.start(new LocationListener() {
@Override
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
}
}, new OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult arg0) {
// TODO Auto-generated method stub
}
});
}
@Override
protected void onStop() {
super.onStop();
locationService.stop();
}
最后,LocationsCoreModule:
public class LocationsCoreModule implements
GooglePlayServicesClient.ConnectionCallbacks {
private static final String TAG = "LocationsCoreModule";
public interface LocationsCoreModuleCallback {
public void locationClientConnected();
}
private LocationsCoreModuleCallback locationsCoreModuleCallback;
private com.google.android.gms.location.LocationListener locationListener;
private Location lastLocation;
private LocationClient mLocationClient;
private final LocationRequest mLocationRequest;
private final Context context;
@Inject
public LocationsCoreModule(Context context, LocationRequest locationRequest) {
this.context = context;
this.mLocationRequest = locationRequest;
}
public void setLastLocation(Location lastLocation) {
this.lastLocation = lastLocation;
}
public void setLocationsListener(
LocationsCoreModuleCallback locationsCoreModuleCallback) {
this.locationsCoreModuleCallback = locationsCoreModuleCallback;
}
public void start(
com.google.android.gms.location.LocationListener locationListener,
GooglePlayServicesClient.OnConnectionFailedListener connectionFailedListener) {
this.locationListener = locationListener;
mLocationClient = new LocationClient(context, this,
connectionFailedListener);
mLocationClient.connect();
}
public void stop() {
if (mLocationClient != null) {
// If the client is connected
if (mLocationClient.isConnected() && locationListener != null) {
/*
* Remove location updates for a listener. The current Activity
* is the listener, so the argument is "this".
*/
mLocationClient.removeLocationUpdates(locationListener);
}
// Disconnecting the client invalidates it.
mLocationClient.disconnect();
}
}
public boolean isConnected() {
if (mLocationClient == null) return false;
return mLocationClient.isConnected();
}
public Location getLastLocation() {
if (lastLocation != null) {
return lastLocation;
}
if (mLocationClient != null) {
if (mLocationClient.isConnected()) {
return lastLocation = mLocationClient.getLastLocation();
}
if (!mLocationClient.isConnecting())
mLocationClient.connect();
}
return null;
}
@Override
public void onConnected(Bundle connectionHint) {
Log.d(TAG, "GooglePlayServices connected!");
Location lastLocation = mLocationClient.getLastLocation();
if (lastLocation == null)
Log.e(TAG, "Lastlocation is null even after connected!!!!");
if (locationsCoreModuleCallback != null) {
locationsCoreModuleCallback.locationClientConnected();
locationsCoreModuleCallback = null; // single shot
}
}
public void requestLocationUpdates() {
if (mLocationClient != null && mLocationClient.isConnected()) {
Log.d(TAG, "Requesting location updates");
mLocationClient.requestLocationUpdates(mLocationRequest,
locationListener);
}
}
public void stopLoactionUpdates() {
if (mLocationClient != null && mLocationClient.isConnected()) {
mLocationClient.removeLocationUpdates(locationListener);
}
}
@Override
public void onDisconnected() {
Log.d(TAG, "GooglePlayServices disconnected!");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.