getLastKnownLocation始终返回null

时间:2014-03-06 11:37:42

标签: android null

在回顾了有关Stack Overflow的一些文章之后,我无法解决我的问题。像往常一样,我总是得到getLastKnownLocation的回报。我也实现了LocationListener。下面是代码。

为了您的信息,我在下面的代码中检查了两个提供商(网络和GPS)并且他们总是返回false,但是如果我检查sBestProvider的值,那么我得到“网络”。它是怎么来的?

权限。

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />  

JAVA代码

public class ActivityMarkingLocation extends Activity implements LocationListener {

GoogleMap oMap ;
LocationManager oLocationManager;
ConnectionDetector oConnectionDetector ;
Location oLocation;
Button oBtnGetCurrentLocation;
String sBestProvider;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_marking_location);

    oLocationManager = (LocationManager)  getSystemService(Context.LOCATION_SERVICE);
    Criteria oCriteria = new Criteria();
    sBestProvider = oLocationManager.getBestProvider(oCriteria, false);
    boolean b =oLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    System.out.println("GPS Status " + b);

    boolean c = oLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    System.out.println("Network Status " + c);
    System.out.println("Provider " + sBestProvider);        
    oLocation = oLocationManager.getLastKnownLocation(sBestProvider);
    System.out.println("Provider : " + sBestProvider);
    System.out.println("LocationManager : " + oLocationManager);

    if (oLocation == null)
        System.out.println("Location Null " );
    else
        System.out.println("Location not null");

    oBtnGetCurrentLocation = (Button) findViewById(R.id.btnGetCurrentLocation);
    oBtnGetCurrentLocation.setOnClickListener(new OnClickListener() {

        @Override   
        public void onClick(View v) {

            oLocation = oLocationManager.getLastKnownLocation(sBestProvider);

            if (oLocation != null)
                System.out.println("Location found");
            if (oLocation != null)
                System.out.println("Position : " + oLocation.getLatitude() + "  " + oLocation.getLongitude());


        }
    });

}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    oLocationManager.requestLocationUpdates(sBestProvider, 600, 10, this);



}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_landing_screen, menu);
    return true;
}
public boolean isGoogleMapsInstalled()
{
    try
    {
        ApplicationInfo info = getPackageManager().getApplicationInfo("com.google.android.apps.maps", 0 );
        return true;
    } 
    catch(PackageManager.NameNotFoundException e)
    {
        return false;
    }
}

@Override
public void onLocationChanged(Location arg0) {
    // TODO Auto-generated method stub
    oLocation = arg0;
    System.out.println("Location changed");
}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}
}

2 个答案:

答案 0 :(得分:0)

修改:试试这个Solution for you

您是在模拟器上还是在手机上运行应用?如果您在模拟器上运行,则必须模拟您的虚拟设备的gps输入。 Extended answer for you 要连接到控制台,请打开命令行并键入:

telnet localhost 5554  

然后,您可以使用geo命令在使用gps位置提供程序传递给所有程序的设备上设置纬度,经度和所需高度。有关详细说明,请参阅上面的链接。

在控制台中运行的特定命令是:

geo fix <longitude value> <latitude value>

也许这会有所帮助link here

答案 1 :(得分:0)

如果你想从家里开发,也许你的GPS没有捕捉到信号。

此外,您可能已禁用GPS。

尝试使用其他提供商 - WiFi或GSM。