package android.test;
import java.util.Iterator;
import android.app.Activity;
import android.location.Criteria;
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.location.GpsStatus.Listener;
import android.location.GpsStatus;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class TestActivity extends Activity implements LocationListener
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
Log.i(tag, "on Create");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn=(Button)findViewById(R.id.btn);
tv1=(TextView)findViewById(R.id.tv1);
tv2=(TextView)findViewById(R.id.tv1);
listener = new GpsStatus.Listener() {
public void onGpsStatusChanged(int event) {
gpsstatus=mgr.getGpsStatus(null);
switch(event)
{
case GpsStatus.GPS_EVENT_FIRST_FIX:gpsstatus.getTimeToFirstFix();
case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
//得到所有收到的卫星的信息,包括 卫星的高度角、方位角、信噪比、和伪随机号(及卫星编号)
Iterable<GpsSatellite> allSatellites;
allSatellites = gpsstatus.getSatellites();
Iterator it=allSatellites.iterator();
String msg="";
while(it.hasNext())
{
GpsSatellite oSat = (GpsSatellite) it.next() ;
msg="azimuth:"+oSat.getAzimuth();
msg+="\nprn:"+oSat.getPrn();
msg+="\nsnr:"+oSat.getSnr();
}
tv2.setText(msg);
break;
case GpsStatus.GPS_EVENT_STARTED:
//Event sent when the GPS system has started.
break;
case GpsStatus.GPS_EVENT_STOPPED:
//Event sent when the GPS system has stopped.
break;
default :
break;
}
}
};
btn.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.i(tag, "on button click");
getdata();
show();
}
});
}
void show()
{
String msg="";
msg+="latitude:"+latitude;
msg+="\nlongtitude:"+longtitude;
msg+="\naltitude:"+altitude;
msg+="\naccuracy:"+accuracy;
msg+="\nbearing:"+bearing;
msg+="\nspeed:"+speed;
msg+="\ntime:"+time;
tv1.setText(msg);
//Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
GpsStatus.Listener listener;
private GpsStatus gpsstatus;
private final String tag="TEST_MAY";
private LocationManager mgr;
private Button btn;
private TextView tv1,tv2;
private String best;
private double latitude;
private double longtitude;
private double altitude;
private float accuracy;
private float bearing;
private float speed;
private long time;
private void getdata()
{
try
{mgr=(LocationManager)getSystemService(LOCATION_SERVICE);
mgr.addGpsStatusListener(listener);
Criteria criteria= new Criteria();
best=mgr.getBestProvider(criteria, true);
Location location= mgr.getLastKnownLocation("gps");
if(location.hasAccuracy()) accuracy=location.getAccuracy();
Log.d(tag, "getdata");
if(location.hasAltitude()) altitude=location.getAltitude();
if(location.hasBearing()) bearing=location.getBearing();
if(location.hasSpeed()) speed=location.getSpeed();
Log.d(tag, "getsomedata");
latitude=location.getLatitude();
longtitude=location.getLongitude();
time=location.getTime();
}
catch( NullPointerException e)
{
}
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
Toast.makeText(this, location.toString(), Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(this, provider+" disable", Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(this, provider+" enable", Toast.LENGTH_LONG).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
Toast.makeText(this, status+" changed", Toast.LENGTH_SHORT).show();
}
}
分享到:
相关推荐
微信小程序-实现电子围栏-半径-经纬度-是否在围栏内-画圆等
按键发送GPS经纬度--STM32-SIM808,供大家学习
public void onClick(View v) { // TODO Auto-generated method stub ... case GpsStatus.GPS_EVENT_SATELLITE_STATUS://获取卫星信息 Iterable<GpsSatellite> allSatellites;
行政区划数据库_with 经纬度-省市区-邮编-区号-拼音-简称
android gps 获取经纬度 代码简单可用,只要一点击按钮,就打印出所在地点的经纬度。
java地址经纬度查询-百度接口
主要内容为通过手机gps获取经纬度数值,设置sqlite数据库,可以将获取的经纬度保存在数据库中,做了简单的数据滤波,可以求得一点的平均经纬度,并获取该点的其他值与均值的距离,从而观察经纬度的精确度。
获取经纬度已经封装成工具类,纯原生获取android手机经纬度,并且根据经纬度转换成准确的地址
Android中实现获得手机当前的经纬度
行政区划数据库_with+经纬度-省市区-邮编-区号-拼音-简称
获取经纬度已经封装成工具类,纯原生获取android手机经纬度,并且根据经纬度转换成准确的地址
android 调用系统GPS获取经纬度
本程序是获取经纬度和位置信息,并将信息存入文件,文件存入sdcard。其中使用了数据库存取,和文件读写。可以供需要学习定位方面的学习参考,共同学习,进行技术交流。
在Android应用中实现GPS返回Location经纬度值 不为null 可以实现
行政区划数据库_with 经纬度-省市区-邮编-区号-拼音-简称
2016省市县行政区划数据库_(经纬度-省市区-邮编-区号-拼音-简称-首字母)
完美封装的Android GPS定位(获取经纬度)管理类,详情见:http://blog.csdn.net/u012810020/article/details/52517976