天天看點

Gps擷取衛星個數

package cn.ckt.factorymode;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.app.Activity;
import android.content.ComponentName;
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.os.Handler;
import android.provider.Settings;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class FactoryAutoGPSTest extends Activity implements OnClickListener{
    public static final String KEY="gps";
    private Button failBtn;
    private Button reTestBtn;
    private Button passBtn;
    private int isPassButtonPress;
    private TextView tv_satellites;
    LocationManager locationManagerExt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.factory_auto_gps_ext);
        commonButtonListener();
        isPassButtonPress = ;
        tv_satellites = (TextView)this.findViewById(R.id.tv_satellites);

        //tv_gps = (TextView) this.findViewById(R.id.tv_gps);

        openGPSSettings();
        tv_satellites.setText("0");

        locationManagerExt = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        // 通過GPS定位
        String provider = LocationManager.GPS_PROVIDER;
        Location location = locationManagerExt.getLastKnownLocation(provider);      
        // 設定監聽器,設定自動更新間隔這裡設定1000ms,移動距離:1米�?
        LocationListener ll = new LocationListener() {

            @Override
            public void onLocationChanged(Location location) {

            }

            @Override
            public void onStatusChanged(String provider, int status,
                    Bundle extras) {

            }

            @Override
            public void onProviderEnabled(String provider) {

            }

            @Override
            public void onProviderDisabled(String provider) {

            }
        };
        locationManagerExt.requestLocationUpdates(provider, , , ll);
        ///
        // 設定狀态監聽回調函數。statusListener是監聽的回調函數�?
        locationManagerExt.addGpsStatusListener(statusListener);


    }
    private void openGPSSettings() {
        LocationManager alm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        if (alm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
            Toast.makeText(this, getString(R.string.gps_worked), Toast.LENGTH_SHORT).show();
            return;
        }

        Toast.makeText(this, getString(R.string.gps_open), Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivityForResult(intent, ); // 此為設定完成後傳回到擷取界面
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == ) {
            this.finish();
        }
        return super.onKeyDown(keyCode, event);
    }
    /**
     * 衛星狀态監聽器
     */
    private List<GpsSatellite> numSatelliteList = new ArrayList<GpsSatellite>(); // 衛星信号

    private final GpsStatus.Listener statusListener = new GpsStatus.Listener() {
        public void onGpsStatusChanged(int event) { // GPS狀态變化時的回調,如衛星數
            LocationManager locationManager = 
                    (LocationManager) FactoryAutoGPSTest.this.getSystemService(Context.LOCATION_SERVICE);
            GpsStatus status = locationManager.getGpsStatus(null); //取目前狀�?
            String satelliteInfo = updateGpsStatus(event, status);

            tv_satellites.setText(null);                
            tv_satellites.setText(satelliteInfo);   

            if((numSatelliteList.size() >= ) &&
                    isPassButtonPress == ){
                //直接使用handler,這裡可以更新UI,原因是new Handler()相當于new Handler(getMainLooper())  
                //下面的Log列印的是main 說明還是運作在主線程�? 
                locationManagerExt.removeGpsStatusListener(statusListener);
                new Handler().postDelayed(new Runnable(){  
                  @Override  
                  public void run() {  
                  // TODO Auto-generated method stub  
                      gpsPass();
                  }  
                }, );  
            }
        }
    };

    private void gpsPass(){
        String classString = FactoryCommonItem.findNextClass(this,KEY);
        Intent intent = new Intent();

        ComponentName component = new ComponentName("cn.ckt.factorymode",classString);
        intent.setComponent(component);
        this.startActivity(intent);
        isPassButtonPress = ;
        this.finish();              
    }

    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<GpsSatellite> it = status.getSatellites().iterator();
            numSatelliteList.clear();
            int count = ;
            while (it.hasNext() && count <= maxSatellites) {
                GpsSatellite s = it.next();
                numSatelliteList.add(s);
                count++;
            }
            //sb2.append(" " + count);
            sb2.append(" " + numSatelliteList.size());
        } else {
            sb2.append(" 0");           
        }

        return sb2.toString();
    }   

    private void commonButtonListener(){
        failBtn = (Button)this.findViewById(R.id.factory_aoto_gps_fail_btn_id);
        //reTestBtn = (Button)this.findViewById(R.id.factory_aoto_gps_retest_btn_id);
        passBtn = (Button)this.findViewById(R.id.factory_aoto_gps_pass_btn_id);
        failBtn.setOnClickListener(this);
        //reTestBtn.setOnClickListener(this);
        passBtn.setOnClickListener(this);       
    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()){
        case R.id.factory_aoto_gps_fail_btn_id:
            FactoryCommonItem.writeSP(this, KEY, , false); // 1: pass, 2:fail, 0: not test
            break;
        //case R.id.factory_aoto_gps_retest_btn_id:
        //  break;
        case R.id.factory_aoto_gps_pass_btn_id:
            FactoryCommonItem.writeSP(this, KEY, , false); // 1: pass, 2:fail, 0: not test
            gpsPass();
            break;
        default:
            break;
        }
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub

        super.onDestroy();

    }
    @Override
    protected void onPause() {
        // TODO Auto-generated method stub

        super.onPause();
    }
    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        locationManagerExt.removeGpsStatusListener(statusListener);
        super.onStop();
    }

}
           

本文轉載自:

作者:ROPHEE

點選連結檢視轉載博文