android gps 获取方位_android 通过GPS获取用户地理位置并监听位置变化

本文介绍了如何在Android中使用LocationManager获取GPS地理位置,并监听位置变化。重点讲解了LocationManager的重要方法getLastKnownLocation,该方法返回设备最后一次已知的位置,但可能存在过期情况。同时提到了权限在manifest.xml中的添加方式。
摘要由CSDN通过智能技术生成

也可通过单击manifest.xml文件的permission标签可视化的添加许可

/**********************************************************************************************************************/

第二部分  认识LocationManager

官方SDK解释“This class provides access to the system location services. These services allow applications to obtain periodic updates of the device's geographical location, or to fire an application-specified Intent

when the device enters the proximity of a given geographical location.

You do not instantiate this class directly; instead, retrieve it through Context.getSystemService(Context.LOCATION_SERVICE). ”

翻译一下大概意思是“LocationManager这个类提供了对系统位置服务的访问,这些服务允许应用程序获取设备地理位置的定期的更新,也可以在设备接近一个指定的地理位置的时候发起一个指定activity的intent。你不需要创建LocationManager的实例,取而代之的是通过Context.getSystemService(Context.LOCATION_SERVICE)获取。”

重要的方法:来自官方SDK(附上我的翻译注释)

-----------------------------------------------------------------------------------------------------------------------------------------------

public Location getLastKnownLocation (String provider)

Since: API Level 1

Returns a Location indicating the data from the last known location fix obtained from the given provider. This can be done without starting the provider. Note that this location could be out-of-date, for example

if the device was turned off and moved to another location.

//返回一个Location,这个location标明从给定provider获得最后已知的位置,也就是最近获取的位置。这个操作可以不必要启动provider,注意这个地址可能是已经过期的,例如使用的设备可能已经被关闭或者转向了另一个位置。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
获取 Android 设备的方位,可以使用 Android 提供的 LocationManager 类和 SensorManager 类。具体步骤如下: 1. 在 AndroidManifest.xml 文件中添加以下权限: ``` <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> ``` 2. 获取 LocationManager 和 SensorManager 实例: ``` LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); SensorManager sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); ``` 3. 注册方向传感器监听器: ``` Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION); sensorManager.registerListener(sensorEventListener, sensor, SensorManager.SENSOR_DELAY_NORMAL); ``` 4. 注册位置监听器: ``` locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); ``` 5. 在方向传感器监听器中获取方向角度: ``` SensorEventListener sensorEventListener = new SensorEventListener() { @Override public void onSensorChanged(SensorEvent event) { if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) { float degree = event.values[0]; // 处理方向角度 } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } }; ``` 6. 在位置监听器中获取设备位置: ``` LocationListener locationListener = new LocationListener() { @Override public void onLocationChanged(Location location) { double latitude = location.getLatitude(); double longitude = location.getLongitude(); // 处理设备位置 } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } }; ``` 通过以上步骤,可以获取 Android 设备的方位。需要注意的是,获取方位需要设备支持方向传感器和 GPS 功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值