Service 内部的 Handler 和 Thread 保持 运行,而 Service 似乎已停止

Handler and Thread inside Service keeps running, while the Service seems to have stopped

我有一个 Service,它每 5 秒使用 HandlerThread 到 运行 一些代码。当我调用 stopSelf() 时,服务似乎已停止,因为从设备设置 -> 应用程序 -> 运行 它没有显示在那里。 但是线程内的代码每 5 秒保持 运行ning,尽管我已经停止了服务。
为什么即使服务已停止,HandlerThread 仍然存在?当服务停止时,如何停止 HandlerThread

final Handler h = new Handler();
final int delay = 5000; // milliseconds

h.postDelayed(new Runnable() {
        public void run() {
            // do something
            final SharedPreferences sharedPref = getSharedPreferences(
                    getString(R.string.prefs), Context.MODE_PRIVATE);
            if (sharedPref != null) {

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        // SOME CODE
                        Log.d("UPDATING"," something here");
                        isUpdated = updateToServer(data);
                        if(isUpdated) {
                            sharedPref.edit().putBoolean("updated",true).commit();
                            stopSelf();
                        }
                    }
                }).start();
            }
            h.postDelayed(this, delay);
        }
    }, delay);

我认为你必须在停止服务之前调用 myHandler.getLooper().quit()

你应该在posting Runnable之前验证isUpdated,如果isUpdated==true,不要再次post,代码如下:

final Handler h = new Handler();
final int delay = 5000; // milliseconds

h.postDelayed(new Runnable() {
        public void run() {
            // do something
            final SharedPreferences sharedPref = getSharedPreferences(
                    getString(R.string.prefs), Context.MODE_PRIVATE);
            if (sharedPref != null) {

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        // SOME CODE
                        Log.d("UPDATING"," something here");
                        isUpdated = updateToServer(data);
                        if(isUpdated) {
                            sharedPref.edit().putBoolean("updated",true).commit();
                            stopSelf();
                        }
                    }
                }).start();
            }
            if(!isUpdated){
               h.postDelayed(this, delay);
            }

        }
    }, delay);

顾名思义,Thread 运行 与 Service 分开,所以它不会仅仅通过停止服务而停止。我实际上并没有测试这段代码,但它应该会停止你的线程和处理程序,或者让你大致了解如何去做。

final Handler h = new Handler();
final int delay = 5000; // milliseconds
private boolean isRunning = true;  // a variable to signal stopping the thread

h.postDelayed(new Runnable() {
    public void run() {
        // do something
        final SharedPreferences sharedPref = getSharedPreferences(
                getString(R.string.prefs), Context.MODE_PRIVATE);
        if (sharedPref != null) {

            new Thread(new Runnable() {
                @Override
                public void run() {
                  if (isRunning) {
                    // SOME CODE
                    Log.d("UPDATING"," something here");
                    isUpdated = updateToServer(data);
                    if(isUpdated) {
                        sharedPref.edit().putBoolean("updated",true).commit();
                        isRunning = false;  // used to stop the thread from continuing the work
                        h.removeCallbacks(this);  // stopping handler ("this" is the handler runnable you are stopping)
                        stopSelf();
                    }
                  }
                }
            }).start();
        }
        h.postDelayed(this, delay);
    }
}, delay);

希望这对你有帮助...