package com.pdager.gps;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.GpsSatellite;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.text.format.Time;
import android.view.KeyEvent;
import android.widget.TextView;
import android.widget.Toast;


public class getlocation extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
openGPSSettings();
}


private void openGPSSettings() {
LocationManager alm = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
if (alm
.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
Toast.makeText(this, "GPS模块正常", Toast.LENGTH_SHORT).show();
getLocation();
return;
}
else{
Toast.makeText(this, "请开启GPS!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
startActivityForResult(intent, R.layout.main); // 此为设置完成后返回到获取界面
getLocation();
return;
}
}


LocationManager locationManager;


private void getLocation() {
// 获取位置管理服务


String serviceName = Context.LOCATION_SERVICE;
locationManager = (LocationManager) this.getSystemService(serviceName);
// 查找到服务信息
// Criteria criteria = new Criteria();
// criteria.setAccuracy(Criteria.ACCURACY_FINE);
// // 高精度
// criteria.setAltitudeRequired(false);
// criteria.setBearingRequired(false);
// criteria.setCostAllowed(true);
// criteria.setPowerRequirement(Criteria.POWER_LOW);
// // 低功耗
// String provider = locationManager.getBestProvider(criteria, true);
// 获取GPS信息
String provider = LocationManager.GPS_PROVIDER;


Location location = locationManager.getLastKnownLocation(provider);// 通过GPS获取位置


if (location == null)
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);


updateToNewLocation(location);
// 设置监听器,自动更新的最小时间为间隔N秒(1秒为1*1000)或最小位移变化超过N米
locationManager.requestLocationUpdates(provider, 1000, 0,
locationListener);
locationManager.addGpsStatusListener(statusListener); // 注册状态信息回调
}


private List<GpsSatellite> numSatelliteList = new ArrayList<GpsSatellite>(); // 卫星信号
/**
* 卫星状态监听器
*/
private final GpsStatus.Listener statusListener = new GpsStatus.Listener() {
public void onGpsStatusChanged(int event) { // GPS状态变化时的回调,如卫星数
GpsStatus status = locationManager.getGpsStatus(null); // 取当前状态
updateGpsStatus(event, status);
}
};


private void updateGpsStatus(int event, GpsStatus status) {
if (event == GpsStatus.GPS_EVENT_SATELLITE_STATUS) {
int maxSatellites = status.getMaxSatellites();
Iterator<GpsSatellite> it = status.getSatellites().iterator();
numSatelliteList.clear();
int count = 0;
while (it.hasNext() && count <= maxSatellites) {
GpsSatellite s = it.next();
numSatelliteList.add(s);
count++;
}


}
}


private void updateToNewLocation(Location location) {
// 获取系统时间
Time t = new Time();
t.setToNow(); // 取得系统时间
int year = t.year;
int month = t.month + 1;
int date = t.monthDay;
int hour = t.hour; // 24小时制
int minute = t.minute;
int second = t.second;
TextView tv1;
tv1 = (TextView) this.findViewById(R.id.tv1);
if (location != null) {
double latitude = location.getLatitude();// 经度
double longitude = location.getLongitude();// 纬度
double altitude = location.getAltitude(); // 海拔
tv1.setText("搜索卫星个数:" + numSatelliteList.size() + "\n纬度:"
+ latitude + "\n经度:" + longitude + "\n海拔:" + altitude
+ "\n时间:" + year + "年" + month + "月" + date + "日" + hour
+ ":" + minute + ":" + second);


} else {


tv1.setText("无法获取地理信息");
}
}


private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// 当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发
if (location != null) {
updateToNewLocation(location);
Toast.makeText(getlocation.this, "您的位置已发生改变!",
Toast.LENGTH_SHORT).show();
}
}


public void onProviderDisabled(String provider) {
// Provider被disable时触发此函数,比如GPS被关闭
updateToNewLocation(null);
}


public void onProviderEnabled(String provider) {
// Provider被enable时触发此函数,比如GPS被打开
}


public void onStatusChanged(String provider, int status, Bundle extras) {
// Provider的转态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
}
};


public boolean onKeyDown(int keyCode, KeyEvent event) {


if (keyCode == KeyEvent.KEYCODE_MENU) {// 拦截menu键事件
// do something...
System.exit(0);


}


if (keyCode == KeyEvent.KEYCODE_BACK) {// 拦截返回按钮事件
// do something...
System.exit(0);
}
return true;
}
}

更多相关文章

  1. android判断网络状态
  2. Android全透明状态栏
  3. Android(安卓)获取屏幕分辨率
  4. 获取Android各种系统信息
  5. Android(安卓)获取手机联系人信息
  6. Android之获取、设置EditText的文本...
  7. Android中MAC地址获取代码
  8. 分享方法:android 获得屏幕状态
  9. 取android:versionCode

随机推荐

  1. 每一位Android开发者应该知道的Android体
  2. 【安全】Android(安卓)O 中的 Seccomp 过
  3. Android预优化那些事
  4. 【Android(安卓)文件管理】分区存储 ( 修
  5. Unity和Android交互笔记——接入Android(
  6. Android一日游
  7. Android(安卓)控件布局常用属性
  8. Android(安卓)鲜为人知的 8 个小秘密
  9. [Android]Android部分基础学习路线图
  10. android企业级商城源码、360°全景图VR源