为什么这适用于Android 1.5而不是1.6(位置管理器)

时间:2010-03-06 20:03:23

标签: android locationmanager

我有这段代码;

 lm = (LocationManager) 
 getSystemService(Context.LOCATION_SERVICE);

 gpslocation = Double.toString(lm.getLastKnownLocation("gps").getLatitude()) +" " 
 + Double.toString(lm.getLastKnownLocation("gps").getLongitude());

在运行android 1.5的模拟器和我的英雄上都可以正常工作,但它在1.6的模拟器上强制关闭,也在我的纹身上。

从1.5改为1.6?

好的,改为使用它;

    lm = (LocationManager) 
    getSystemService(Context.LOCATION_SERVICE);

    Double latPoint = null;
    Double lngPoint = null;

    Location loc = lm.getLastKnownLocation("gps");
    if(loc != null) {
       latPoint = lm.getLastKnownLocation("gps").getLatitude();
       lngPoint = lm.getLastKnownLocation("gps").getLongitude();

    } else {

    }

  Toast.makeText(getBaseContext(),"test lat " + latPoint, Toast.LENGTH_LONG).show();

如果我在运行应用程序之前在模拟器上触发一个位置,我会得到null toast和null toast。

1 个答案:

答案 0 :(得分:2)

通常,在Eclipse中使用adb logcat,DDMS或DDMS透视图来查看与“强制关闭”对话框关联的Java堆栈跟踪,以查看问题所在。

在具体情况下,由于您未在代码段中打开GPS,因此无法使用。

除非该提供程序已启动并正在运行,否则无法在提供程序上可靠地调用getLastKnownLocation()。在您的情况下,GPS可能无法正常运行,getLastKnownLocation()将返回null

getLastKnownLocation()工作之前,您需要注册位置更新或接近警报,以启动GPS无线电并寻求修复。此外,使用模拟器,您需要在注册位置更新后提交修复(例如,通过DDMS),或者getLastKnownLocation()之前将返回非null值的内容。