android GPS 获取卫星数量

  1. import java.util.ArrayList;  
  2. import java.util.Iterator;  
  3. import java.util.List;  
  4.   
  5. import android.app.Activity;  
  6. import android.content.Context;  
  7. import android.content.Intent;  
  8. import android.location.GpsSatellite;  
  9. import android.location.GpsStatus;  
  10. import android.location.Location;  
  11. import android.location.LocationListener;  
  12. import android.location.LocationManager;  
  13. import android.os.Bundle;  
  14. import android.provider.Settings;  
  15. import android.view.KeyEvent;  
  16. import android.view.View;  
  17. import android.widget.Button;  
  18. import android.widget.TextView;  
  19. import android.widget.Toast;  
  20.   
  21. /** 
  22.  * @author liuzhidong 
  23.  * @create date 2012-08 
  24.  * @modifydate 2012.09.24 
  25.  */  
  26. public class GPSActivity extends Activity {  
  27.   
  28.     private TextView tv_gps, tv_satellites;  
  29.     private Button bt_Quit;  
  30.     LocationManager locationManager;  
  31.     private StringBuilder sb;  
  32.   
  33.     @Override  
  34.     protected void onCreate(Bundle savedInstanceState) {  
  35.         super.onCreate(savedInstanceState);  
  36.         setContentView(R.layout.activity_gps);  
  37.   
  38.         tv_satellites = (TextView)this.findViewById(R.id.tv_satellites);  
  39.         tv_gps = (TextView) this.findViewById(R.id.tv_gps);  
  40.         bt_Quit = (Button) this.findViewById(R.id.bt_quit_gps);  
  41.   
  42.         openGPSSettings();  
  43.   
  44.         LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);  
  45.         String provider = LocationManager.GPS_PROVIDER;  
  46.         Location location = locationManager.getLastKnownLocation(provider);  
  47.         updateMsg(location);  
  48.   
  49.         LocationListener ll = new LocationListener() {  
  50.   
  51.             @Override  
  52.             public void onLocationChanged(Location location) {  
  53.                 String locInfo = updateMsg(location);  
  54.                 tv_gps.setText(null);  
  55.                 tv_gps.setText(locInfo);  
  56.             }  
  57.   
  58.             @Override  
  59.             public void onStatusChanged(String provider, int status,  
  60.                     Bundle extras) {  
  61.   
  62.             }  
  63.   
  64.             @Override  
  65.             public void onProviderEnabled(String provider) {  
  66.   
  67.             }  
  68.   
  69.             @Override  
  70.             public void onProviderDisabled(String provider) {  
  71.   
  72.             }  
  73.   
  74.         };  
  75.   
  76.         locationManager.requestLocationUpdates(provider, 10001, ll);  
  77.   
  78.         bt_Quit.setOnClickListener(new View.OnClickListener() {  
  79.             @Override  
  80.             public void onClick(View v) {  
  81.                 GPSActivity.this.finish();  
  82.             }  
  83.         });  
  84.   
  85.         locationManager.addGpsStatusListener(statusListener);  
  86.     }  
  87.   
  88.     private String updateMsg(Location loc) {  
  89.         sb = null;  
  90.         sb = new StringBuilder("位置信息:\n");  
  91.         if (loc != null) {  
  92.             double lat = loc.getLatitude();  
  93.             double lng = loc.getLongitude();  
  94.   
  95.             sb.append("纬度:" + lat + "\n经度:" + lng);  
  96.   
  97.             if (loc.hasAccuracy()) {  
  98.                 sb.append("\n精度:" + loc.getAccuracy());  
  99.             }  
  100.   
  101.             if (loc.hasAltitude()) {  
  102.                 sb.append("\n海拔:" + loc.getAltitude() + "m");  
  103.             }  
  104.   
  105.             if (loc.hasBearing()) {// 偏离正北方向的角度  
  106.                 sb.append("\n方向:" + loc.getBearing());  
  107.             }  
  108.   
  109.             if (loc.hasSpeed()) {  
  110.                 if (loc.getSpeed() * 3.6 < 5) {  
  111.                     sb.append("\n速度:0.0km/h");  
  112.                 } else {  
  113.                     sb.append("\n速度:" + loc.getSpeed() * 3.6 + "km/h");  
  114.                 }  
  115.   
  116.             }  
  117.         } else {  
  118.             sb.append("没有位置信息!");  
  119.         }  
  120.   
  121.         return sb.toString();  
  122.     }  
  123.   
  124.     private void openGPSSettings() {  
  125.         LocationManager alm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);  
  126.         if (alm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {  
  127.             Toast.makeText(this"GPS模块正常", Toast.LENGTH_SHORT).show();  
  128.             return;  
  129.         }  
  130.   
  131.         Toast.makeText(this"请开启GPS!", Toast.LENGTH_SHORT).show();  
  132.         Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);  
  133.         startActivityForResult(intent, 0); // 此为设置完成后返回到获取界面  
  134.     }  
  135.   
  136.     public boolean onKeyDown(int keyCode, KeyEvent event) {  
  137.         if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {  
  138.             GPSActivity.this.finish();  
  139.         }  
  140.         return super.onKeyDown(keyCode, event);  
  141.     }  
  142.   
  143.     /** 
  144.      * 卫星状态监听器 
  145.      */  
  146.     private List<GpsSatellite> numSatelliteList = new ArrayList<GpsSatellite>(); // 卫星信号  
  147.       
  148.     private final GpsStatus.Listener statusListener = new GpsStatus.Listener() {  
  149.         public void onGpsStatusChanged(int event) { // GPS状态变化时的回调,如卫星数  
  150.             LocationManager locationManager = (LocationManager) GPSActivity.this.getSystemService(Context.LOCATION_SERVICE);  
  151.             GpsStatus status = locationManager.getGpsStatus(null); //取当前状态  
  152.             String satelliteInfo = updateGpsStatus(event, status);  
  153.             tv_satellites.setText(null);  
  154.             tv_satellites.setText(satelliteInfo);  
  155.         }  
  156.     };  
  157.   
  158.     private String updateGpsStatus(int event, GpsStatus status) {  
  159.         StringBuilder sb2 = new StringBuilder("");  
  160.         if (status == null) {  
  161.             sb2.append("搜索到卫星个数:" +0);  
  162.         } else if (event == GpsStatus.GPS_EVENT_SATELLITE_STATUS) {  
  163.             int maxSatellites = status.getMaxSatellites();  
  164.             Iterator<GpsSatellite> it = status.getSatellites().iterator();  
  165.             numSatelliteList.clear();  
  166.             int count = 0;  
  167.             while (it.hasNext() && count <= maxSatellites) {  
  168.                 GpsSatellite s = it.next();  
  169.                 numSatelliteList.add(s);  
  170.                 count++;  
  171.             }  
  172.             sb2.append("搜索到卫星个数:" + numSatelliteList.size());  
  173.         }  
  174.           
  175.         return sb2.toString();  
  176.     }  
  177. }  
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值