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权限。

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


    
    

获得一个位置管理的引用

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






你可能感兴趣的:(Android)