【问题标题】:getLastKnownLocation always returns nullgetLastKnownLocation 始终返回 null
【发布时间】:2014-03-06 11:37:41
【问题描述】:

在查看有关 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

}
}

【问题讨论】:

  • 为什么我的代码不起作用?
  • 解决了,我的错。设置中未勾选“使用无线网络”。感谢所有花费宝贵时间为我提供解决方案的人。

标签: android null


【解决方案1】:

编辑:试试这个Solution for you

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

telnet localhost 5554  

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

在控制台中运行的具体命令是:

geo fix <longitude value> <latitude value>

也许这会有所帮助link here

【讨论】:

  • 在三星 Galaxy Tab 2 上运行应用程序
  • 在那里尝试解决方案链接。
  • 试试这个link
【解决方案2】:

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

另外,您可能禁用了 GPS。

尝试使用其他提供商 - WiFi 或 GS​​M。

【讨论】:

  • 设备 wifi 已打开,甚至显示“网络”作为提供商。但是,从早上开始就面临这个奇怪的问题。
  • 是的,Wifi 已开启,但最好的提供商显然是 GPS。 GPS 可能无法在某些地方获取您的位置。
猜你喜欢
  • 1970-01-01
  • 2013-11-06
  • 2013-12-24
  • 2012-05-28
  • 1970-01-01
  • 2015-08-20
相关资源
最近更新 更多