位置信息在日志中输出
package com.mytest; import android.app.Activity; import android.app.ProgressDialog; import android.content.Context; import android.content.Intent; import android.location.Criteria; import android.location.GpsStatus; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.location.LocationProvider; import android.os.Bundle; import android.provider.Settings; import android.util.Log; import android.widget.Toast; public class LocationActivity extends Activity { private static final String LOG_TAG = "LocationActivity"; private LocationManager locationManager; @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); locationManager.removeUpdates(locationListener); } @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); // 获取manager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { Toast.makeText(this, "请打开GPS", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(Settings.ACTION_LOCALE_SETTINGS); startActivityForResult(intent, 0); return; } else { // 创建查询条件 Criteria criteria = new Criteria(); // 设置定位精确度 Criteria.ACCURACY_COARSE比较粗略,Criteria.ACCURACY_FINE则比较精细 criteria.setAccuracy(Criteria.ACCURACY_FINE); // 设置是否要求速度 criteria.setSpeedRequired(false); // 设置是否允许运营商收费 criteria.setCostAllowed(false); // 设置是否需要方位信息 criteria.setBearingRequired(false); // 设置是否需要海拔信息 criteria.setAltitudeRequired(false); // 设置对电源的需求 criteria.setPowerRequirement(Criteria.POWER_LOW); String bestProvider = locationManager.getBestProvider(criteria, true); Location location = locationManager.getLastKnownLocation(bestProvider); if (location != null) { double longitude = location.getLongitude(); // 纬度 double latitude = location.getLatitude(); // 经度 Log.i(LOG_TAG, "经度:" + latitude + ",纬度:" + longitude); } locationManager.addGpsStatusListener(gpsListener); // 绑定监听,有4个参数 // 参数1,设备:有GPS_PROVIDER和NETWORK_PROVIDER两种 // 参数2,位置信息更新周期,单位毫秒 // 参数3,位置变化最小距离:当位置距离变化超过此值时,将更新位置信息 // 参数4,监听 // 备注:参数2和3,如果参数3不为0,则以参数3为准;参数3为0,则通过时间来定时更新;两者为0,则随时刷新 // 3秒更新一次,或最小位移变化超过1米更新一次; // 注意:此处更新准确度非常低,推荐在service里面启动一个Thread,在run中sleep(10000);然后执行handler.sendMessage(),更新位置 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 1, locationListener); } } private LocationListener locationListener = new LocationListener() { @Override public void onStatusChanged(String provider, int status, Bundle extras) { switch (status) { // GPS状态为可见时 case LocationProvider.AVAILABLE: Log.i(LOG_TAG, "当前GPS状态为可见状态"); break; // GPS状态为服务区外时 case LocationProvider.OUT_OF_SERVICE: Log.i(LOG_TAG, "当前GPS状态为服务区外状态"); break; // GPS状态为暂停服务时 case LocationProvider.TEMPORARILY_UNAVAILABLE: Log.i(LOG_TAG, "当前GPS状态为暂停服务状态"); break; } } @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) { Log.v(LOG_TAG, "GPS位置发生改变," + "时间:" + location.getTime() + "经度:" + location.getLongitude() + "纬度:" + location.getLatitude() + "海拔:" + location.getAltitude()); } }; GpsStatus.Listener gpsListener = new GpsStatus.Listener() { @Override public void onGpsStatusChanged(int event) { switch (event) { case GpsStatus.GPS_EVENT_FIRST_FIX: Log.v(LOG_TAG, "第一次定位"); break; case GpsStatus.GPS_EVENT_SATELLITE_STATUS: Log.v(LOG_TAG, "卫星状态发生改变"); break; case GpsStatus.GPS_EVENT_STARTED: Log.v(LOG_TAG, "启动定位"); break; case GpsStatus.GPS_EVENT_STOPPED: Log.v(LOG_TAG, "结束定位"); break; } } }; }
时间: 2024-10-25 01:53:49