GPSDemo-手动获取
package com.lilin.gps;
import java.util.Date;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
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.text.TextUtils;
import android.text.format.DateFormat;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class GPSView extends Activity implements OnClickListener {
private LocationManager locationManager;
private Criteria criteria;
private String provider;
private TextView tv_isgps;// gps的状态
private TextView tv_Y;// 纬度
private TextView tv_X;// 经度
private TextView tv_GpsTime;// 获取的时间
private TextView tv_InfoType;// 信息的来源
private EditText et_SetTimeSpace;// 设置时间间隔
private Button get_btn;
private Button setime_btn;
private Button setgps_btn;
int getTime = 5000;// 每5秒获得一次
int distance = 10;// 距离10米以上
GPS gps = new GPS();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 隐藏输入法
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
setTitle("GPS");
initUI();
}
/**
* @author lilin
* @date 2012-2-21 下午10:04:06
* @annotation
*/
private void initUI() {
tv_isgps = (TextView) findViewById(R.id.tv_isgps);
tv_Y = (TextView) findViewById(R.id.tvlatitude);
tv_X = (TextView) findViewById(R.id.tvlongitude);
tv_GpsTime = (TextView) findViewById(R.id.tvgpstime);
tv_InfoType = (TextView) findViewById(R.id.tvinfotype);
et_SetTimeSpace = (EditText) findViewById(R.id.ettimespace);
get_btn = (Button) findViewById(R.id.get_btn);
setgps_btn = (Button) findViewById(R.id.setgps_btn);
get_btn.setOnClickListener(this);
setime_btn = (Button) findViewById(R.id.settime_btn);
setime_btn.setOnClickListener(this);
setgps_btn.setOnClickListener(this);
}
/**
*
* @author lilin
* @date 2012-2-21 下午10:23:27
* @annotation 检查GPS模块
*/
private boolean checkGPS() {
LocationManager lm = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);// 获取位置管理服务
if (lm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
Toast.makeText(this, "GPS模块正常", 5000).show();
tv_isgps.setText("GPS模块正常");
return true;
}
tv_isgps.setText("GPS模块已关闭!");
clearDate();
Toast.makeText(GPSView.this, "请开启GPS!", 5000).show();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("GPS未打开,要打开吗?");
builder.setTitle("提示");
builder.setCancelable(false).setPositiveButton("确定",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// 打开GPS设置
Intent intent = new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent, 0); // 此为设置完成后返回到获取界面
}
});
builder.setNegativeButton("取消", null).show();
return false;
}
// 初始化
private void initGPS() {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);// 高精度
criteria.setAltitudeRequired(true);// 显示海拔
criteria.setBearingRequired(false);// 显示方向
criteria.setSpeedRequired(false);// 显示速度
criteria.setCostAllowed(false);// 不允许有花费
criteria.setPowerRequirement(Criteria.POWER_LOW);// 低功耗
provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider); // 通过GPS获取位置
getGPS(location);
locationManager.requestLocationUpdates(provider, getTime, distance,
locationListener);// 位置变化监听
}
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location arg0) {
// showInfo(getGPS(), 2);
}
public void onProviderDisabled(String arg0) {
// showInfo(null, -1);
}
public void onProviderEnabled(String arg0) {
}
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
};
private GPS getGPS(Location location) {// 获取GPS相应的数据
if (location != null) {
gps.Latitude = location.getLatitude();
gps.Longitude = location.getLongitude();
Date d = new Date();
d.setTime(location.getTime() + 28800000);// UTC时间,转北京时间+8小时
gps.GpsTime = DateFormat.format("yyyy-MM-dd kk:mm:ss", d)
.toString();
d = null;
}
return gps;
}
// 显示信息
private void showInfo(GPS gps, int infotype) {
if (gps != null) {
tv_Y.setText("y=" + gps.Latitude);
tv_X.setText("x=" + gps.Longitude);
tv_GpsTime.setText("更新时间:" + gps.GpsTime);
switch (gps.InfoType) {
case 1:
tv_InfoType.setText("信息来源状态:手动获取更新");
break;
case 2:
tv_InfoType.setText("信息来源状态:位置改变更新");
break;
}
} else {
tv_X.setText("GPS数据获取失败,请稍后再试!");
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.get_btn:
if (checkGPS()) {
initGPS();
if (gps != null) {
showInfo(gps, 1);
}
}
break;
case R.id.settime_btn:
String text = et_SetTimeSpace.getText().toString();
if (TextUtils.isEmpty(text)) {
Toast.makeText(this, "请输入更新时间间隔", Toast.LENGTH_LONG).show();
et_SetTimeSpace.requestFocus();
return;
}
Toast.makeText(this, "设置成功 :每" + text + "秒更新", 5000).show();
getTime = Integer.valueOf(text) * 1000;
if (locationManager.isProviderEnabled(Context.LOCATION_SERVICE))
locationManager.requestLocationUpdates(provider, getTime,
distance, locationListener);
break;
case R.id.setgps_btn:
// 打开GPS设置
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent, 0); // 此为设置完成后返回到获取界面
break;
default:
break;
}
}
public void clearDate() {
tv_GpsTime.setText("");
tv_InfoType.setText("");
tv_X.setText("");
tv_Y.setText("");
}
public void onResume() {
LocationManager lm = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);// 获取位置管理服务
if (lm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
Toast.makeText(this, "GPS模块正常", 5000).show();
tv_isgps.setText("GPS模块正常");
} else {
tv_isgps.setText("GPS模块已关闭!");
clearDate();
}
super.onResume();
}
public void onPause() {
LocationManager lm = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);// 获取位置管理服务
if (lm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
Toast.makeText(this, "GPS模块正常", 5000).show();
tv_isgps.setText("GPS模块正常");
} else {
tv_isgps.setText("GPS模块已关闭!");
clearDate();
}
super.onPause();
}
}