java] view plaincopyprint?java
import java.util.ArrayList; android
import java.util.Iterator; git
import java.util.List; app
import android.app.Activity; ide
import android.content.Context; ui
import android.content.Intent; this
import android.location.GpsSatellite; spa
import android.location.GpsStatus; .net
import android.location.Location; blog
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
/**
* @author liuzhidong
* @create date 2012-08
* @modifydate 2012.09.24
*/
public class GPSActivity extends Activity {
private TextView tv_gps, tv_satellites;
private Button bt_Quit;
LocationManager locationManager;
private StringBuilder sb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gps);
tv_satellites = (TextView)this.findViewById(R.id.tv_satellites);
tv_gps = (TextView) this.findViewById(R.id.tv_gps);
bt_Quit = (Button) this.findViewById(R.id.bt_quit_gps);
openGPSSettings();
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
String provider = LocationManager.GPS_PROVIDER;
Location location = locationManager.getLastKnownLocation(provider);
updateMsg(location);
LocationListener ll = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
String locInfo = updateMsg(location);
tv_gps.setText(null);
tv_gps.setText(locInfo);
}
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
locationManager.requestLocationUpdates(provider, 1000, 1, ll);
bt_Quit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GPSActivity.this.finish();
}
});
locationManager.addGpsStatusListener(statusListener);
}
private String updateMsg(Location loc) {
sb = null;
sb = new StringBuilder("位置信息:\n");
if (loc != null) {
double lat = loc.getLatitude();
double lng = loc.getLongitude();
sb.append("緯度:" + lat + "\n經度:" + lng);
if (loc.hasAccuracy()) {
sb.append("\n精度:" + loc.getAccuracy());
}
if (loc.hasAltitude()) {
sb.append("\n海拔:" + loc.getAltitude() + "m");
}
if (loc.hasBearing()) {// 偏離正北方向的角度
sb.append("\n方向:" + loc.getBearing());
}
if (loc.hasSpeed()) {
if (loc.getSpeed() * 3.6 < 5) {
sb.append("\n速度:0.0km/h");
} else {
sb.append("\n速度:" + loc.getSpeed() * 3.6 + "km/h");
}
}
} else {
sb.append("沒有位置信息!");
}
return sb.toString();
}
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();
return;
}
Toast.makeText(this, "請開啓GPS!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
startActivityForResult(intent, 0); // 此爲設置完成後返回到獲取界面
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
GPSActivity.this.finish();
}
return super.onKeyDown(keyCode, event);
}
/**
* 衛星狀態監聽器
*/
private List<GpsSatellite> numSatelliteList = new ArrayList<GpsSatellite>(); // 衛星信號
private final GpsStatus.Listener statusListener = new GpsStatus.Listener() {
public void onGpsStatusChanged(int event) { // GPS狀態變化時的回調,如衛星數
LocationManager locationManager = (LocationManager) GPSActivity.this.getSystemService(Context.LOCATION_SERVICE);
GpsStatus status = locationManager.getGpsStatus(null); //取當前狀態
String satelliteInfo = updateGpsStatus(event, status);
tv_satellites.setText(null);
tv_satellites.setText(satelliteInfo);
}
};
private String updateGpsStatus(int event, GpsStatus status) {
StringBuilder sb2 = new StringBuilder("");
if (status == null) {
sb2.append("搜索到衛星個數:" +0);
} else 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++;
}
sb2.append("搜索到衛星個數:" + numSatelliteList.size());
}
return sb2.toString();
}
}