Android 位置管理器(LocationManager)

Android LocationManager的提供了一系列与地理位置相关的服务,包括允许应用获取设备的周期性的位置更新;以及接近某个地理位置坐标时触发一个已定义Intent的等。只需要进行一些简单的设置,你的应用程序就可以接受位置更新,在这次教程里你将详细的学习这些步骤。
在Manifest里声明相关的权限
要想获取位置更新,第一步需要在manifest里声明合适的权限。如果忘了声明相应的权限,那么你的应用在运行时会报安全异常。当你使用LocationManagement方法的时候,需要设置权限ACCESS_CORASE_LOCATION或者 ACCESS_FINE_LOCATION,例如,如果你的应用使用了基于网络的信息服务,你需要声明ACCESS_CORASE_LOATION权限,要想获取GPS请求你需要声明ACCESS_FINE_LOCATION权限。值得注意的是如果你声明了ACCESS_FINE_LOCATION权限隐含着你也声明了ACCESS_CORASE_LOCATION权限。

假如一个应用使用了基于网络的位置的信息服务,你需要声明因特网权限。

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

获得一个位置管理的引用

LocationManager是Android系统提供的一项服务。使用方法类似于其他的服务,通过调用getSystemService方法可以获得相应的引用。

LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

选择一个位置提供者
当没有请求的时候,现在大部分android电源管理可以通过多种底层技术可以获得位置更新,这种技术被抽象为LocationProvider类的应用。在时间、精度、成本、电源消耗等方面,位置提供者有不同的运行特性。通常,GPS位置提供者定位精确度高,花费时间长,基于网络的位置提供者定位精确度低,但是花费时间比较短。

通过权衡之后你必须选择一种特殊的位置提供者,或者多重提供者,这些都依赖与你的应用的客户需求。例如,比如说一个关键点的签到服务,需要高精度定位,而一个零售商店定位器使用城市级别的修正就可以满足。下面的代码段要求一个GPS提供者的支持。

LocationProvider provider = locationManager.getProvider(LocationManager.GPS_PROVIDER);

你可以提供一些输入标准,比如精度、功率需求、成本等等,让android决定一个最合适的位置匹配提供者。下边的代码片段需要的是更精确的位置提供者而不是考虑成本。需要注意的是这个标准不能帮你解决任何的提供者,可能返回值为空。这个时候你的应用应该能够很好的处理这种情况:

// Retrieve a list of location providers that have fine accuracy, no monetary cost, etc
		Criteria criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_FINE);
		criteria.setCostAllowed(false);
//		criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
//		criteria.setBearingAccuracy(Criteria.ACCURACY_LOW);
		
		String providerName = locationManager.getBestProvider(criteria, true);

		// If no suitable provider is found, null is returned.
		if (providerName != null) {
		   Log.i(TAG, "BestProvider name:"+providerName);
		}

检查位置提供者是否启用
在设置里,一些位置提供者比如GPS可以被关闭。良好的做法就是通过调用isProviderEnabled()方法来检测你想要的位置提供者是否打开。如果位置提供者被关闭了,你可以在设置里通过启动Intent来让用户打开。

@Override
	protected void onStart() {
		
		boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
		if (!gpsEnabled) {
	        // Build an alert dialog here that requests that the user enable
	        // the location services, then when the user clicks the "OK" button,
	        // call enableLocationSettings()
	    }
		
		super.onStart();
	}
	
	private void enableLocationSettings() {
	    Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
	    startActivity(settingsIntent);
	}	

获取周期性位置更新

通过LocationManager requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener)获取周期性位置更新。

/**
	 * determines location using satellites. Depending on conditions, this provider may take a while to return a location fix. 
	 * Requires the permission android.Manifest.permission.ACCESS_FINE_LOCATION. 
	 */
	private void requestLocationByGPS() {
		
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, mLocationListener);
		
		Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);  
		if(location!=null){
			double lat = location.getLatitude();     //经度  
			double lng = location.getLongitude(); //纬度  
			double altitude =  location.getAltitude();     //海拔  
			
			Log.i(TAG, "LastKnownLocation latitude="+lat+",longitude="+lng+",altitude="+altitude);
		}
	}
	
	private LocationListener mLocationListener = new LocationListener() {
		
		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onProviderEnabled(String provider) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onProviderDisabled(String provider) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onLocationChanged(Location location) {
			if(location==null){
				return;
			}
			double lat = location.getLatitude();     //经度  
			double lng = location.getLongitude(); //纬度  
			double altitude =  location.getAltitude();     //海拔  
			
			Log.e(TAG, "onLocationChanged lat="+lat+",lng="+lng+",altitude="+altitude);
		}
	};

停止更新

通过LocationManager removeUpdates(LocationListener listener)方法来停止更新。

locationManager.removeUpdates(mLocationListener);

最后附上完整示例代码:

package com.example.gpsdemo;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {

	private static final String TAG = MainActivity.class.getSimpleName();
	
	private Button bt_gps;
	private Button bt_network;
	private Button bt_stop;
	
	private LocationManager locationManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		findViewById();
		setListener();
		processLogic();
	}

	private void processLogic() {
		locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
		
		// Retrieve a list of location providers that have fine accuracy, no monetary cost, etc
		Criteria criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_FINE);
		criteria.setCostAllowed(false);
//		criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
//		criteria.setBearingAccuracy(Criteria.ACCURACY_LOW);
		
		String providerName = locationManager.getBestProvider(criteria, true);

		// If no suitable provider is found, null is returned.
		if (providerName != null) {
		   Log.i(TAG, "BestProvider name:"+providerName);
		}
	}

	private void findViewById() {
		bt_gps = (Button) findViewById(R.id.bt_gps);
		bt_network = (Button) findViewById(R.id.bt_network);
		bt_stop = (Button) findViewById(R.id.bt_stop);
	}
	
	private void setListener() {
		bt_gps.setOnClickListener(this);
		bt_network.setOnClickListener(this);
		bt_stop.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bt_gps:
			
			requestLocationByGPS();
			
			break;
		case R.id.bt_network:
			requestLocationByNetwork();
			break;
		case R.id.bt_stop:
			
			//Removes all location updates for the specified LocationListener.
			locationManager.removeUpdates(mLocationListener);
			
			break;
		default:
			break;
		}
	}
	
	@Override
	protected void onStart() {
		
		boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
		if (!gpsEnabled) {
	        // Build an alert dialog here that requests that the user enable
	        // the location services, then when the user clicks the "OK" button,
	        // call enableLocationSettings()
	    }
		
		super.onStart();
	}
	
	private void enableLocationSettings() {
	    Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
	    startActivity(settingsIntent);
	}	

	/**
	 *  determines location based on availability of cell tower and WiFi access points. 
	 *  Requires the permission android.Manifest.permission.INTERNET 
	 */
	private void requestLocationByNetwork() {
		
		locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, mLocationListener);
		
		Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);  
		if(location!=null){
			double lat = location.getLatitude();     //经度  
			double lng = location.getLongitude(); //纬度  
			double altitude =  location.getAltitude();     //海拔  
			
			Log.i(TAG, "LastKnownLocation latitude="+lat+",longitude="+lng+",altitude="+altitude);
		}
	}

	/**
	 * determines location using satellites. Depending on conditions, this provider may take a while to return a location fix. 
	 * Requires the permission android.Manifest.permission.ACCESS_FINE_LOCATION. 
	 */
	private void requestLocationByGPS() {
		
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, mLocationListener);
		
		Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);  
		if(location!=null){
			double lat = location.getLatitude();     //经度  
			double lng = location.getLongitude(); //纬度  
			double altitude =  location.getAltitude();     //海拔  
			
			Log.i(TAG, "LastKnownLocation latitude="+lat+",longitude="+lng+",altitude="+altitude);
		}
	}
	
	private LocationListener mLocationListener = new LocationListener() {
		
		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onProviderEnabled(String provider) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onProviderDisabled(String provider) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onLocationChanged(Location location) {
			if(location==null){
				return;
			}
			double lat = location.getLatitude();     //经度  
			double lng = location.getLongitude(); //纬度  
			double altitude =  location.getAltitude();     //海拔  
			
			Log.e(TAG, "onLocationChanged lat="+lat+",lng="+lng+",altitude="+altitude);
		}
	};

	@Override
	protected void onStop() {
		Log.i(TAG, "onStop");
		locationManager.removeUpdates(mLocationListener);
		super.onStop();
	}
	
}


关于LocationManager的详细介绍可以参考Developer文档:http://developer.android.com/reference/android/location/LocationManager.html






  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!
Android 应用中,获取位置权限可以通过以下步骤实现: 1. 在 AndroidManifest.xml 文件中添加位置权限声明。例如: ``` <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> ``` 2. 在代码中请求位置权限,并处理用户的授权结果。例如: ``` // 检查是否已授权 if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // 未授权,请求权限 ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSIONS_REQUEST_LOCATION); } else { // 已授权,执行相应操作 // ... } // 处理授权结果 @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case MY_PERMISSIONS_REQUEST_LOCATION: { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // 用户同意授权,执行相应操作 // ... } else { // 用户拒绝授权,提示或执行其他操作 // ... } return; } } } ``` 3. 获取位置信息。例如: ``` LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); if (location != null) { double latitude = location.getLatitude(); double longitude = location.getLongitude(); // ... } ``` 需要注意的是,为了保护用户隐私,Android 6.0 及以上版本对权限进行了更加严格的管理,需要在运行时请求权限。同时,如果用户拒绝了权限请求,应用应该适当处理,例如提示用户需要该权限才能正常使用某些功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值