android 经纬度更新,android gps经纬度实时更新,获取卫星数量

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;

@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 numSatelliteList = new ArrayList(); // 卫星信号

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 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();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值