假如一个应用使用了基于网络的位置的信息服务,你需要声明因特网权限。
LocationManager是Android系统提供的一项服务。使用方法类似于其他的服务,通过调用getSystemService方法可以获得相应的引用。
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
通过权衡之后你必须选择一种特殊的位置提供者,或者多重提供者,这些都依赖与你的应用的客户需求。例如,比如说一个关键点的签到服务,需要高精度定位,而一个零售商店定位器使用城市级别的修正就可以满足。下面的代码段要求一个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);
}
@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