在转载的基础上增加一些内容,主要是时间的转换所以这里直接先把重点列出来:
(GPS时间转为本地时间):
@SuppressLint("SimpleDateFormat")
private static String getGpsLoaalTime(long gpsTime){
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(gpsTime);
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
String datestring = df.format(calendar.getTime());
return datestring;
}
上述代码需要加载Java的包支持,用eclipse会自动加载的,然后是把获取的GPS数据显示在安卓app上,代码如下:
private void updateView(Location location) {
if (location != null) {
editText.setText("设备位置信息\n\n时间:");
editText.append(""+getGpsLoaalTime(location.getTime());
editText.append("\n经度:");
editText.append(String.valueOf(location.getLongitude()));
editText.append("\n纬度:");
editText.append(String.valueOf(location.getLatitude()));
editText.append("\n海拔:");
editText.append(String.valueOf(location.getAltitude()));
} else {
// 清空EditText对象
editText.getEditableText().clear();
}
}
上面数据的里获取时间的那一项里是转换为北京时间,单位是秒,其他的代码,别人有讲的很详细,请参考:
点击打开链接http://blog.csdn.net/jason0539/article/details/12047963
当然这里把整体框架也写出来:
package com.hq.gpstest;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.annotation.SuppressLint;
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.location.LocationProvider;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.widget.EditText;
import android.widget.Toast;
public class GpsActivity extends Activity {
private EditText editText;
private LocationManager lm;
private static final String TAG = "GpsActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gps);
setTitle("GPS离线定位");
editText = (EditText) findViewById(R.id.editText);
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// 判断GPS是否正常启动
if (!lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Toast.makeText(this, "请开启GPS导航...", Toast.LENGTH_SHORT).show();
// 返回开启GPS导航设置界面
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent, 0);
return;
}
// 为获取地理位置信息时设置查询条件
String bestProvider = lm.getBestProvider(getCriteria(), true);
Location location = lm.getLastKnownLocation(bestProvider);
updateView(location);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, locationListener);
}
// 位置监听
private LocationListener locationListener = new LocationListener() {
/**
* 位置信息变化时触发
*/
public void onLocationChanged(Location location) {
updateView(location);
Log.i(TAG, "时间:" + location.getTime());
Log.i(TAG, "经度:" + location.getLongitude());
Log.i(TAG, "纬度:" + location.getLatitude());
Log.i(TAG, "海拔:" + location.getAltitude());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
switch (status) {
// GPS状态为可见时
case LocationProvider.AVAILABLE:
Log.i(TAG, "当前GPS状态为可见状态");
break;
// GPS状态为服务区外时
case LocationProvider.OUT_OF_SERVICE:
Log.i(TAG, "当前GPS状态为服务区外状态");
break;
// GPS状态为暂停服务时
case LocationProvider.TEMPORARILY_UNAVAILABLE:
Log.i(TAG, "当前GPS状态为暂停服务状态");
break;
}
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Location location = lm.getLastKnownLocation(provider);
updateView(location);
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
updateView(null);
}
};
private void updateView(Location location) {
if (location != null) {
editText.setText("设备位置信息\n\n时间:");
editText.append(""+getGpsLoaalTime(location.getTime()+(long)57647030));
editText.append("\n经度:");
editText.append(String.valueOf(location.getLongitude()));
editText.append("\n纬度:");
editText.append(String.valueOf(location.getLatitude()));
editText.append("\n海拔:");
editText.append(String.valueOf(location.getAltitude()));
} else {
// 清空EditText对象
editText.getEditableText().clear();
}
}
private Criteria getCriteria() {
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);
return criteria;
}
@SuppressLint("SimpleDateFormat")
private static String getGpsLoaalTime(long gpsTime){
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(gpsTime);
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
String datestring = df.format(calendar.getTime());
return datestring;
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}
这是一个完整的程序,稍微加工一下就可以用了。