Running GPS positioning in a background thread on Android

void startGPSThread() {
    Thread t = new Thread() {

        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);     
        boolean isDebug = CurrentLogEntry.getInstance().isDebug();

        // Define a listener that responds to location updates
        LocationListener locationListener  = new LocationListener() {

            public void onStatusChanged(String provider, int status, Bundle extras) {
                /* This is called when the GPS status changes */
                String tag = "onStatusChanged, ";
                switch (status) {
                    case LocationProvider.OUT_OF_SERVICE:
                        Log.w(tag, "Status Changed: Out of Service");
                        break;
                    case LocationProvider.TEMPORARILY_UNAVAILABLE:
                        Log.w(tag, "Status Changed: Temporarily Unavailable");
                        break;
                    case LocationProvider.AVAILABLE:
                        break;
                }
            }

            public void onProviderEnabled(String provider) {
            }

            public void onProviderDisabled(String provider) {
                // This is called if the GPS is disabled in settings.
                // Bring up the GPS settings
                Intent intent = new Intent(
                        android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent);
            }

            public void onLocationChanged(Location location) {

                // Once a location has been received, ignore all other position
                // updates.
                locationManager.removeUpdates(locationListener);
                locationManager.removeUpdates(this);

                // Make sure that the received location is a valid one,
                // otherwise show a warning toast and hit "back".
                if (location == null) {
                    String warningString = "Location was unititialized!";

                    if (isDebug) {
                        Toast.makeText(getApplicationContext(),
                                warningString,
                                Toast.LENGTH_LONG).show();
                    }

                    KeyEvent kev = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK);
                    onKeyDown(KeyEvent.KEYCODE_BACK, kev);
                }

                CurrentLogEntry.getInstance().setUserLatitude(location.getLatitude());
                CurrentLogEntry.getInstance().setUserLongitude(location.getLongitude());

                //Send update to the main thread
                int result = 0;
                if (location.getLatitude() == 0 || location.getLongitude() == 0) {
                    result = -1;
                }

                messageHandler.sendMessage(Message.obtain(messageHandler, result));
            }
        };

        @Override
        public void run() {

            locationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER, 0, 0, locationListener);

            // Wait until a position has bee acquired.
            while(!CurrentLogEntry.getInstance().isReadyToCalculate()){
                try {
                    wait(250);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    };

    t.start();
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值