android gps經緯度即時更新,擷取衛星數量

來源:互聯網
上載者:User
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.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;@Overrideprotected 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() {@Overridepublic void onLocationChanged(Location location) {String locInfo = updateMsg(location);tv_gps.setText(null);tv_gps.setText(locInfo);}@Overridepublic void onStatusChanged(String provider, int status,Bundle extras) {}@Overridepublic void onProviderEnabled(String provider) {}@Overridepublic void onProviderDisabled(String provider) {}};locationManager.requestLocationUpdates(provider, 1000, 1, ll);bt_Quit.setOnClickListener(new View.OnClickListener() {@Overridepublic 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();}}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.