如何在 Android 中获取位置管理器的最后一个已知位置?

How to get last known location for Location manager in Android?(如何在 Android 中获取位置管理器的最后一个已知位置?)
本文介绍了如何在 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 中获取位置管理器的最后一个已知位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How can I use CClistview in COCOS2d Android?(如何在 COCOS2d Android 中使用 CClistview?)
cocos2d-android: how to display score(cocos2d-android:如何显示分数)
Sqlite database not copied from asset folder Android(Sqlite 数据库未从资产文件夹 Android 复制)
SQLite Database Copy Appears Corrupted When Generated by Device and not Emulator(SQLite 数据库副本在由设备而不是模拟器生成时出现损坏)
Android file copy(安卓文件拷贝)
Android how to detect Copy event of Edittext in android(Android如何在android中检测Edittext的Copy事件)