如何在 Android 中获取位置管理器的最后一个已知位置? [英] How to get last known location for Location manager in Android?

查看:21
本文介绍了如何在 Android 中获取位置管理器的最后一个已知位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用简单的 位置管理器 对象来获取设备的 lastKnownLocation()但是在 return 中得到 null object 谁能告诉我为什么?

I am using simple location manager object to get lastKnownLocation() of device but getting null object in return can any one tell me why ?

代码:

public Location getLocation() {
    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);        
    if (locationManager != null) {          
        Location lastKnownLocationGPS = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (lastKnownLocationGPS != null) {             
            return lastKnownLocationGPS;
        } else {                
            Location loc =  locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
            System.out.println("1::"+loc);----getting null over here
            System.out.println("2::"+loc.getLatitude());
            return loc;
        }
    } else {            
        return null;
    }
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.location);
    getLocation();-----calling service

 }

已授予权限:

 <uses-permission android:name="android.permission.INTERNET" />
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 <uses-permission android:name="android.permission.READ_PHONE_STATE" />
 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

所以有什么需要设置的吗?我已检查我的位置服务是否已在设备中打开请提供一些工作示例的链接

so is anything missing to set up? I have checked my location service is on in device please give some links for working examples

推荐答案

这个问题的答案很愚蠢...我重新启动了我的设备...它工作了...请确保您的位置服务已启用并正常工作正确的

Silly thing as answer to this ...I restarted my device ...and it worked...Please ensure that your service for location are enabled and working properly

这篇关于如何在 Android 中获取位置管理器的最后一个已知位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆