【问题标题】:Handler stuck in loop within Android Service处理程序卡在 Android 服务中的循环中
【发布时间】:2015-07-06 23:07:32
【问题描述】:

我目前正在尝试制作一个应用程序,通过使用一项服务通过 GPS 跟踪手机。为了让 GPS 更新坐标,我需要在服务中使用处理程序。现在我遇到的问题是,当我执行 Handler.post 时,它陷入了一个循环,之后,它完全忽略了其余的服务代码。

当我调试时,我发现处理程序在方法之间交替消息,但没有任何有用的结果,它只是一遍又一遍地在相同方法之间循环。

这是包含处理程序的我的服务代码:

   public int onStartCommand(Intent intent, int flags, int startId)
        {
        ctx = ServicioDeFondo.this;
        mHandler = new Handler();


        reportarGPS = new Thread(new Runnable() { public void run()
        {
            try
            {
                while(true)
                {
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            gps = new GPSTrack(ctx);
                            latitude = String.valueOf(gps.getLatitude());
                            longitude = String.valueOf(gps.getLongitude());
                        }
                    });

                    Thread.sleep(10000);

                    try {
                        new APISendClass().execute();
                        Thread.sleep(10000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        catch (Exception e)
        {
            //TODO Auto-generated catch block
            e.printStackTrace();
        }
        } });



        reportarGPS.start();

        return START_STICKY;
    }

我整天都被困在这里,任何帮助将不胜感激!

【问题讨论】:

    标签: android gps android-service handler android-handler


    【解决方案1】:

    根据您对问题的简要描述,很难理解预期的行为是什么。您没有解释 GPSTrackAPISendClass 做什么以及它们是什么类型的对象。你说“它陷入了一个循环”。目前尚不清楚“它”是什么。使用while (true) 语句,线程将循环直到被取消。

    注意Service 方法,例如onStartCommand() 在主线程上运行。这意味着您的 Handler() 构造函数将处理程序与主线程相关联。您发布到该处理程序的可运行文件在主线程上运行。这是你想要的吗?

    还要注意,通过stopSelf()Context.stopService() 停止服务不会杀死线程。当不再需要线程时,您需要有代码来取消线程。这通常在onDestroy() 中完成。

    我采用了您发布的代码,用 Log 语句替换了对未知对象的调用并运行了它。 logcat 输出在“Get lat/long”和“APISendClass()”之间交替。

    Handler mHandler;
    Context ctx;
    Thread reportGPS;
    
    public int onStartCommand(Intent intent, int flags, int startId){
        Log.i("TEST", "onStartCommand()");
    
        ctx = this;
    
        // Service methods run on main thread.
    
        // Handler constructor with no args associates Handler
        // with current thread, which here is the main thread.
        mHandler = new Handler();
    
        reportGPS = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    while (true) {
                        mHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                // This runnable is posted to the main thread.
                                // Is that what you intended?
    
                                //gps = new GPSTrack(ctx);
                                //latitude = String.valueOf(gps.getLatitude());
                                //longitude = String.valueOf(gps.getLongitude());
                                Log.i("TEST", "Get lat/long");
                            }
                        });
                        Thread.sleep(2000);
    
                        try {
                            //new APISendClass().execute();
                            Log.i("TEST", "APISendClass().execute()");
                            Thread.sleep(5000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    
        reportGPS.start();
    
        return START_STICKY;
    }
    

    【讨论】:

      猜你喜欢
      • 2016-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多