得到卫星信息需要了解
GpsSatellite
结构
继承关系
public final class GpsSatellite extends Object
java.lang.Object
android.location.GpsSatellite
类概述
此类用于描述当前GPS卫星状态。和GpsStatus联合使用。
公共方法
public float getAzimuth ()
返回卫星的方位角,方位角范围0至360度。
返回值
卫星的方位角
public float getElevation ()
返回卫星的高度角,高度角范围0至90度
返回值
卫星的高度角
public int getPrn ()
返回伪随机噪声码,
(译者注:所谓伪噪声码,不是卫星编号,是一个具有一定周期的取值0和1的离散符号串,它具有类似于白噪声的自相关函数)
返回值
伪随机噪声码
public float getSnr ()
返回卫星的信噪比。
(译者注:即放大器的输出信号的电压与同时输出的噪声电压的比,常常用分贝数表示。设备的信噪比越高表明它产生的杂音越少。一般来说,信噪比越大,说明混在信号里的噪声越小,声音回放的音质量越高,否则相反)
返回值
卫星信噪比
public boolean hasAlmanac ()
如果GPS引擎有近似轨道信息则返回true
返回值
如果GPS引擎有近似轨道信息则返回true
public boolean hasEphemeris ()
如果GPS引擎有卫星星历则返回true
(译者注:卫星星历用于描述太空飞行体位置和速度的表达式)
返回值
如果GPS引擎有卫星星历则返回true
public boolean useInfix ()
当卫星被GPS引擎用于计算最近位置时,返回true
返回值
当卫星被GPS引擎用于计算最近位置时,返回true
下面是一个例子源码
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.util.Log;
import android.view.KeyEvent;
import android.widget.TextView;
import android.widget.Toast;
public class GetLocationDemo 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_FIRST_FIX) fixStates = true;
if(!fixStates){
Log.v("demo", fixStates+"updateToNewLocation out!");
updateToNewLocation(new Location(""));
}
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 boolean fixStates = false;
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+"\n");
tv1.append("定位状态:"+ fixStates +"\n");
for(int i = 0 ; i < numSatelliteList.size() ; i++){
GpsSatellite gs = numSatelliteList.get(i);
tv1.append("卫星-"+i+" Snr:"+gs.getSnr() + " , "
+" Azimuth:"+gs.getAzimuth() + " , "
+" Elevation:"+gs.getElevation() + " , "
+" Prn:"+gs.getPrn() + " , "
+" Almanac:"+gs.hasAlmanac() + " , "
+" Ephemeris:"+gs.hasEphemeris() + " , "
+"\n");
}
} else {
tv1.setText("无法获取地理信息");
}
}
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// 当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发
if (location != null) {
updateToNewLocation(location);
Toast.makeText(GetLocationDemo.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;
}
}