当前位置:编程学习 > JAVA >>

android GPS JAVA应用程序编程-------获得经纬度,卫星信息等

Java代码 
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(); 
   } 

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,