android - getLastknownLocation() 在 nexus 上返回 null 值

标签 android location

我正在开发基于位置的项目,我使用以下代码

我正在为该项目使用 google api 8

 lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
    currloc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    TextView t = (TextView)findViewById(R.id.textView1);
    try{
        t.setText("Your current location is - "+currloc.getLatitude()+","+currloc.getLongitude());
    }catch (Exception e) {
        // TODO: handle exception
        t.setText("cant find current location ");
    }

此代码在我的 Galaxy Tab 上运行良好,甚至在 HTC 上也运行良好

但是当我使用 Nexus 时,它返回位置的空值。 我是否需要更改我的 API 级别,或者 Galaxy Nexus 有任何具体要求吗 预先感谢您:)

最佳答案

请遵循代码行..

Step1: into your oncreate

LocationListener locationListener = new LocalLocationListener();
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

Step2: into class body

/**Listener on location change*/
private class LocalLocationListener implements LocationListener
{

    public void onLocationChanged(Location location)
    {
        String text = "My current Location is: "+location.getLatitude()+", "+location.getLongitude();
        GeoPoint geoPoint = new GeoPoint((int)(location.getLatitude()* 1E6), (int)(location.getLatitude() * 1E6));
        mapController_.animateTo(geoPoint);
        Toast.makeText(LocalMap.this, text, Toast.LENGTH_SHORT).show();
        Log.i("onLocationChanged", text);

    }

    public void onProviderDisabled(String provider)
    {
        // TODO Auto-generated method stub
        Toast.makeText(LocalMap.this, "GPS Disable", Toast.LENGTH_SHORT).show();
        Log.i("onProviderDisabled", "GPS Disable");
    }

    public void onProviderEnabled(String provider)
    {
        // TODO Auto-generated method stub
        Toast.makeText(LocalMap.this, "GPS Enable", Toast.LENGTH_SHORT).show();
        Log.i("onProviderEnabled", "GPS Enable");
    }

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

    }

关于android - getLastknownLocation() 在 nexus 上返回 null 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10681369/

相关文章:

android - 帮助修复 Android Studio 中的错误

android - 在 android 中获取 uri.parse() 的文件路径

java - LocationRequest 构造函数被标记为内部

java - Android 点击手势

php - 如何高效检测用户所在城市?

java - 调整 jpanel 在 jframe resize 上的位置

android - 如何检测位置提供者? GPS 或网络供应商

Android - 无法将图库图像设置为 ImageView

android - 为什么使用 Graph API 获取 FB 头像需要访问 token ?

java - 当服务调用 AsycTask 内的 Handler 中的 Toast 时,出现 NullPointerException