【问题标题】:How to get current location through Android GPS using Runnables and searching TimeOut?如何使用 Runnables 和搜索 TimeOut 通过 Android GPS 获取当前位置?
【发布时间】:2015-11-05 15:30:39
【问题描述】:

我正在尝试开发一些解决方案来使用 Android 的 GPS 获取我的当前位置。但是,搜索仅限于超时,并且当搜索位置开始时,会有一个对话框允许用户取消搜索。

为此,我正在考虑使用 runnable。下面是负责计时器的代码(我在带有开关盒的状态机中使用它)。

runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    progressDialog = new ProgressDialog(AddPandlet.this);

                    progressDialog.setButton(DialogInterface.BUTTON_NEUTRAL, getString(R.string.addpandlet_cancelsearch),
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    //CANCEL OBTAINING GPS
                                    return;
                                }
                            });
                    progressDialog.show();
                    scanLocation();

                    Timer timer = new Timer();
                    timer.schedule(new TimerTask() {
                        @Override
                        public void run() {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    progressDialog.hide();
                                }
                            });
                            status = Status.FINISH;
                            updateStateMachine();
                        }
                    }, SCAN_PERIOD_GPS);

在 LocationListener 上,我正在这样做:

LocationListener locationListenerNetwork = new LocationListener() {
    public void onLocationChanged(Location location) {
        locationManager.removeUpdates(this);
        locationManager.removeUpdates(locationListenerGps);

        longitude = "Longitude: " + location.getLongitude();
        latitude = "Latitude: " + location.getLatitude();
    }
    public void onProviderDisabled(String provider) {}
    public void onProviderEnabled(String provider) {}
    public void onStatusChanged(String provider, int status, Bundle extras) {}
};

此外,我无法获取 gps 坐标,因为 GPS 一直在监听,并且不会在计时器结束时停止。

有人可以帮我吗?谢谢。

【问题讨论】:

    标签: android timer runnable locationmanager locationlistener


    【解决方案1】:

    删除,

    locationManager.removeUpdates(this);
    locationManager.removeUpdates(locationListenerGps);
        longitude = "Longitude: " + location.getLongitude();
        latitude = "Latitude: " + location.getLatitude();
    

    试试这个

        longitude = "Longitude: " + location.getLongitude();
        latitude = "Latitude: " + location.getLatitude();    
    locationManager.removeUpdates(locationListenerNetwork);
    locationManager = null;
    

    【讨论】:

      猜你喜欢
      • 2016-07-30
      • 1970-01-01
      • 1970-01-01
      • 2014-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-01
      相关资源
      最近更新 更多