GPS不是异步工作任务 [英] GPS not working in Async Task

查看:278
本文介绍了GPS不是异步工作任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了获取的坐标,并将其显示在屏幕上简单的GPS程序。当我试图改善这一设计和实施一个异步任务时,GPS似乎并不出于某种原因或其他工作。有一些问题与问候使用异步任务与GPS?这里是我的code:

 私有类DownloadTask扩展的AsyncTask<弦乐,太虚,对象> {
        保护对象doInBackground(字符串参数... args){
            Log.i(MyApp的,后台线程启动);            的LocationManager mLocationManager;
            Gpslistener Gpslistener;            Gpslistener =新Gpslistener();            尝试{               mLocationManager =(的LocationManager)getSystemService(Context.LOCATION_SERVICE);               mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,Gpslistener);            }赶上(例外五){                test_gps = TRUE;
           }
        返回null;
    }    保护无效onPostExecute(对象结果){        如果(test_gps ==真){
            如果(ParkingActivity.this.progressDialog!= NULL){
                ParkingActivity.this.progressDialog.dismiss();
            }
            AlertMessage(GPS错误,无法获取位置);
        }公共类Gpslistener实现LocationListener的
{
        @覆盖
        公共无效onLocationChanged(位置LOC)
        {
            loc.getLatitude();
            loc.getLongitude();
        }        @覆盖
        公共无效onProviderDisabled(字符串提供商)
        {
        }        @覆盖
        公共无效onProviderEnabled(字符串提供商)
        {
        }        @覆盖
        公共无效onStatusChanged(字符串提供商,INT地位,捆绑演员)
        {
        }
    }

我每次运行它​​,它总是捕捉异常。全球定位系统权限在清单设置和我总是用不同的应用程序,以确保GPS连接线上,所以我消除这些潜在的错误。老实说,我想不出别的,没有异步任务它完美!任何帮助深表AP preciated,谢谢!

编辑
我的例外logcat的:

  05-09 22:56:20.199:E /例外:(8874):了java.lang.RuntimeException:无法内螺纹已不叫尺蠖$ P创建处理程序$ ppare()
05-09 22:56:20.199:E /例外:(8874):了java.lang.RuntimeException:无法内螺纹已不叫尺蠖prepare创造处理器()
05-09 22:56:20.199:E /例外:(8874):在android.os.Handler<&初始化GT;(Handler.java:121)。
05-09 22:56:20.199:E /例外:(8874):在android.location.LocationManager $ ListenerTransport $ 1<&初始化GT;(LocationManager.java:173)
05-09 22:56:20.199:E /例外:(8874):在android.location.LocationManager $ ListenerTransport<&初始化GT;(LocationManager.java:173)。
05-09 22:56:20.199:E /例外:(8874):在android.location.LocationManager._requestLocationUpdates(LocationManager.java:579)
05-09 22:56:20.199:E /例外:(8874):在android.location.LocationManager.requestLocationUpdates(LocationManager.java:446)
05-09 22:56:20.199:E /例外:(8874):在stefan.testservice.ParkingActivity $ DownloadTask.doInBackground(ParkingActivity.java:163)
05-09 22:56:20.199:E /例外:(8874):在stefan.testservice.ParkingActivity $ DownloadTask.doInBackground(ParkingActivity.java:1)
05-09 22:56:20.199:E /例外:(8874):在android.os.AsyncTask $ 2.call(AsyncTask.java:185)
05-09 22:56:20.199:E /例外:(8874):在java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:306)
05-09 22:56:20.199:E /例外:(8874):在java.util.concurrent.FutureTask.run(FutureTask.java:138)
05-09 22:56:20.199:E /例外:(8874):在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
05-09 22:56:20.199:E /例外:(8874):在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:581)
05-09 22:56:20.199:E /例外:(8874):在java.lang.Thread.run(Thread.java:1019)


解决方案

您不能这样做。线程一旦死亡,而 doInBackground()的回报。该GPS监听器是需要一个尺蠖线程操作处理程序。在 doInBackground的开头()您需要调用活套。prepare()然后在结束通话 Looper.loop()。它会永远循环下去,直到你调用退出()对线程的尺蠖对象。

的AsyncTask 真的是专为一次性临时线程的,所以我会使用一个线程正常此。虽然,没有理由你不能,我想。你只需要做出100%确保您结束循环,否则将在用户关闭应用程序即使永远运行。

<一个href=\"http://stackoverflow.com/questions/10403858/java-cant-create-handler-inside-thread-that-has-not-called-looper-$p$ppare/10404099#10404099\">Java不能内螺纹创建处理程序,也没有所谓的活套。prepare()

I have implemented a simple GPS program that fetches the co-ordinates and displays them on the screen. When I tried to improve on this design and implement an async task, the GPS doesn't seem to work for some reason or another. Is there some issue with regards to using async task with GPS? Here is my code:

private class DownloadTask extends AsyncTask<String, Void, Object> {
        protected Object doInBackground(String... args) {
            Log.i("MyApp", "Background thread starting");

            LocationManager mLocationManager;
            Gpslistener Gpslistener;

            Gpslistener = new Gpslistener();

            try{

               mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

               mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,Gpslistener);

            }catch (Exception e) {

                test_gps = true;
           }


        return null;
    }

    protected void onPostExecute(Object result) {

        if(test_gps == true){
            if (ParkingActivity.this.progressDialog != null) {
                ParkingActivity.this.progressDialog.dismiss();
            }               
            AlertMessage("GPS Error", "Unable to get location");
        }



public class Gpslistener implements LocationListener
{
        @Override
        public void onLocationChanged(Location loc)
        {
            loc.getLatitude();
            loc.getLongitude();
        }

        @Override
        public void onProviderDisabled(String provider)
        {       
        }

        @Override
        public void onProviderEnabled(String provider)
        {
        }

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

Each time I run it, it always catches an exception. The GPS permissions are set in the manifest and I'm always using a different application to ensure that a GPS connection is online so I eliminated those as potential errors. Honestly I can't think of anything else, without the async task it works perfectly! Any help is much appreciated, thanks!

Edited My exception logcat:

05-09 22:56:20.199: E/EXCEPTION:(8874): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
05-09 22:56:20.199: E/EXCEPTION:(8874): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
05-09 22:56:20.199: E/EXCEPTION:(8874):     at android.os.Handler.<init>(Handler.java:121)
05-09 22:56:20.199: E/EXCEPTION:(8874):     at android.location.LocationManager$ListenerTransport$1.<init>(LocationManager.java:173)
05-09 22:56:20.199: E/EXCEPTION:(8874):     at android.location.LocationManager$ListenerTransport.<init>(LocationManager.java:173)
05-09 22:56:20.199: E/EXCEPTION:(8874):     at android.location.LocationManager._requestLocationUpdates(LocationManager.java:579)
05-09 22:56:20.199: E/EXCEPTION:(8874):     at android.location.LocationManager.requestLocationUpdates(LocationManager.java:446)
05-09 22:56:20.199: E/EXCEPTION:(8874):     at stefan.testservice.ParkingActivity$DownloadTask.doInBackground(ParkingActivity.java:163)
05-09 22:56:20.199: E/EXCEPTION:(8874):     at stefan.testservice.ParkingActivity$DownloadTask.doInBackground(ParkingActivity.java:1)
05-09 22:56:20.199: E/EXCEPTION:(8874):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
05-09 22:56:20.199: E/EXCEPTION:(8874):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
05-09 22:56:20.199: E/EXCEPTION:(8874):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
05-09 22:56:20.199: E/EXCEPTION:(8874):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
05-09 22:56:20.199: E/EXCEPTION:(8874):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
05-09 22:56:20.199: E/EXCEPTION:(8874):     at java.lang.Thread.run(Thread.java:1019)

解决方案

You can't do that. The thread dies as soon as doInBackground() returns. The GPS listener is a Handler which requires a Looper thread to operate. At the beginning of doInBackground() you need to call Looper.prepare() then at the end call Looper.loop(). It will loop forever until you call quit() on the thread's Looper object.

AsyncTask is really designed for one-shot temporary threads though, so I'd use a normal thread for this. Although, there's no reason you couldn't I suppose. You just have to make 100% sure that you end the loop or it will run forever even after the user closes the app.

Java Can't create handler inside thread that has not called Looper.prepare()

这篇关于GPS不是异步工作任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆