Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android上的后台线程中运行GPS定位_Android_Multithreading_Gps - Fatal编程技术网

在Android上的后台线程中运行GPS定位

在Android上的后台线程中运行GPS定位,android,multithreading,gps,Android,Multithreading,Gps,我有一个奇怪的问题,在我的应用程序中,GPS定位需要相当长的时间,因为它往往这样做,因此我想运行它作为自己的线程,而用户正在其他视图中进行选择 我的结构是,我有一个在后台始终处于活动状态的主视图,然后向用户显示一系列视图,用户可以在这些视图中进行一些选择。最后,根据所做的选择和当前位置向用户显示结果。所有这些都是可行的,但我想将GPS位移动到它自己的线程,这样用户就不必等待它完成 用户的所有选择和GPS坐标都存储在名为CurrentLogEntry的单例中。程序不同部分之间的所有通信都通过它来执

我有一个奇怪的问题,在我的应用程序中,GPS定位需要相当长的时间,因为它往往这样做,因此我想运行它作为自己的线程,而用户正在其他视图中进行选择

我的结构是,我有一个在后台始终处于活动状态的主视图,然后向用户显示一系列视图,用户可以在这些视图中进行一些选择。最后,根据所做的选择和当前位置向用户显示结果。所有这些都是可行的,但我想将GPS位移动到它自己的线程,这样用户就不必等待它完成

用户的所有选择和GPS坐标都存储在名为CurrentLogEntry的单例中。程序不同部分之间的所有通信都通过它来执行

我在main视图中创建了handleMessageMessage msg override,然后在main.java中实现了此函数:

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();
}
此函数在Main.onCreate中调用

不幸的是,这根本不起作用。程序到达locationManager.requestLocationUpdatesLocationManager.GPS_提供程序0,0,locationListener后立即崩溃;这让我完全不知所措


有人能告诉我为什么这个不会作为后台线程运行吗?另外,我真的需要在最后进行等待轮询以确保线程在收到数据之前保持活动状态吗?在我想把它放进自己的线程之前,GPS位工作得很好,那么我到底做错了什么?

据我所知,为了在后台运行位置更新,你必须使线程成为一个活套线程……但是我自己仍在试图弄明白这一点……当我弄明白如何做时,我会发布它

可能与我收到的错误相同,即无法在尚未调用Looper.prepare的线程中创建处理程序


希望你能让它工作起来。据我所知,为了在后台运行位置更新,你必须使线程成为一个活套线程。不过,我自己仍在努力解决这个问题。当我找到解决方法时,我会发布它

可能与我收到的错误相同,即无法在尚未调用Looper.prepare的线程中创建处理程序


希望你能让它工作起来,检查线程,特别是答案。它们处理相同的主题、位置和相同的问题,从线程循环运行。

检查线程,特别是寻找答案。它们处理相同的主题、位置和相同的问题,从线程循环运行。

您可以发布错误日志吗?我很乐意,但没有。我得到的只是应用程序意外停止。请再试一次。Netbeans没有给我任何输出。编辑:是否有我遗漏的其他输出?Netbeans调试器控制台仅包含有关我的断点和运行用户程序完成项的用户程序的消息。请发布错误日志好吗?我很乐意,但没有。我得到的只是应用程序意外停止。请再试一次。Netbeans没有给我任何输出。编辑:是否有我遗漏的其他输出?Netbeans调试器控制台仅包含有关我的断点和运行用户程序完成项的用户程序的消息。