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 = 0;
        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, 1000, 1, 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, 0); // 此为设置完成后返回到获取界面
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            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() >= 4) &&
                    isPassButtonPress == 0){
                //直接使用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();
                  }  
                }, 1000);  
            }
        }
    };

    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 = 1;
        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 = 0;
            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, 2, 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, 1, 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
点击链接查看转载博文

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值