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)    {        }        }   @Overridepublic void onLocationChanged(Location location) {// TODO Auto-generated method stubToast.makeText(this, location.toString(), Toast.LENGTH_SHORT).show();}@Overridepublic void onProviderDisabled(String provider) {// TODO Auto-generated method stubToast.makeText(this, provider+" disable", Toast.LENGTH_SHORT).show();}@Overridepublic void onProviderEnabled(String provider) {// TODO Auto-generated method stubToast.makeText(this, provider+" enable", Toast.LENGTH_LONG).show();}@Overridepublic void onStatusChanged(String provider, int status, Bundle extras) {// TODO Auto-generated method stubToast.makeText(this, status+" changed", Toast.LENGTH_SHORT).show();}}

更多相关文章

  1. webview高度自适应图文详情
  2. Android(安卓)获取NavigationBar高度
  3. android -- 小功能 Android为多媒体文件生成缩略图
  4. Android(安卓)自定义分享列表ACTION_SEND
  5. android获取屏幕的宽度和高度
  6. Android点击按钮实现全屏的代码
  7. android 利用startActivityForResult()方法得到另一个Activity的
  8. android 图片点击一下就放大到全屏,再点一下就回到原界面
  9. android获取手机屏幕的宽度及高度

随机推荐

  1. [iOS初级教程之三]Crash分析实践
  2. 如何让 iPhone 自带日历 App 显示国家法
  3. Oracle OCP 071中文考试题库-第15题
  4. 随笔
  5. 阿里云盘来袭,送几个福利码!手慢无!
  6. 什么是增强分析?增强分析如何改善传统BI工
  7. C语言中static 试题
  8. 选择器的优先级,id,class,tag。前端组件
  9. 浅谈CSS选择器以及伪类选择器模块化
  10. php如何用get方式写一个简单的API接口并