Java Android service onDestroy方法在调用stopService()后延迟运行

Java Android service onDestroy方法在调用stopService()后延迟运行,java,android,service,delay,ondestroy,Java,Android,Service,Delay,Ondestroy,我正在使用一个服务来运行一个计时器,该计时器由主活动控制。但是,调用stopService()之后的代码行似乎在服务中的onDestroy()方法之前运行 以下是调用stopService()的MainActivityClass中的代码: 其中runTimerService()是: public void runTimerService(){ sharedPreferences.edit().putInt("intTimeOnBegin", (int) SystemClock.elaps

我正在使用一个服务来运行一个计时器,该计时器由主活动控制。但是,调用
stopService()
之后的代码行似乎在服务中的
onDestroy()方法之前运行

以下是调用
stopService()
的MainActivityClass中的代码:

其中
runTimerService()
是:

public void runTimerService(){
    sharedPreferences.edit().putInt("intTimeOnBegin", (int) SystemClock.elapsedRealtime()).apply();
    startService(intentTimerService);
}
    handler = new Handler();
    runnable = new Runnable() {
        @Override
        public void run() {
            //RUNNABLE

            //Behaviour
            if(sharedPreferences.getBoolean("isSSelected", true)){
                //RUNS IF S IS SELECTED

                //Behaviour
                intTime = sharedPreferences.getInt("intSTime", 0) + ((int) SystemClock.elapsedRealtime() - sharedPreferences.getInt("intTimeOnBegin", 0));
                sharedPreferences.edit().putString("stringSTime", formatTime(intTime)).apply();
                sharedPreferences.edit().putString("stringPTime", formatTime(sharedPreferences.getInt("intPTime", 0))).apply();
            }else{
                //RUNS IF P IS SELECTED

                //Behaviour
                intTime = sharedPreferences.getInt("intPTime", 0) + ((int) SystemClock.elapsedRealtime() - sharedPreferences.getInt("intTimeOnBegin", 0));
                sharedPreferences.edit().putString("stringPTime", formatTime(intTime)).apply();
                sharedPreferences.edit().putString("stringSTime", formatTime(sharedPreferences.getInt("intSTime", 0))).apply();
            }

            //Set runnable repeat interval
            handler.postDelayed(this, 1);
        }
    };
这是ServiceClass中的
onDestroy()
方法:

其中
runnable
为:

public void runTimerService(){
    sharedPreferences.edit().putInt("intTimeOnBegin", (int) SystemClock.elapsedRealtime()).apply();
    startService(intentTimerService);
}
    handler = new Handler();
    runnable = new Runnable() {
        @Override
        public void run() {
            //RUNNABLE

            //Behaviour
            if(sharedPreferences.getBoolean("isSSelected", true)){
                //RUNS IF S IS SELECTED

                //Behaviour
                intTime = sharedPreferences.getInt("intSTime", 0) + ((int) SystemClock.elapsedRealtime() - sharedPreferences.getInt("intTimeOnBegin", 0));
                sharedPreferences.edit().putString("stringSTime", formatTime(intTime)).apply();
                sharedPreferences.edit().putString("stringPTime", formatTime(sharedPreferences.getInt("intPTime", 0))).apply();
            }else{
                //RUNS IF P IS SELECTED

                //Behaviour
                intTime = sharedPreferences.getInt("intPTime", 0) + ((int) SystemClock.elapsedRealtime() - sharedPreferences.getInt("intTimeOnBegin", 0));
                sharedPreferences.edit().putString("stringPTime", formatTime(intTime)).apply();
                sharedPreferences.edit().putString("stringSTime", formatTime(sharedPreferences.getInt("intSTime", 0))).apply();
            }

            //Set runnable repeat interval
            handler.postDelayed(this, 1);
        }
    };

因此,在MainActivity中调用
onClickLayoutS()
后,它应该将
intTime
的值保存为
“intSTime”
在SharedReferences中。但是,情况并非如此,当我运行应用程序并调用
onClickLayoutS()
时,它会将
intTime
的值保存为
“intPTime”
。为什么会这样?提前感谢您的帮助:)

方法是异步的,并且会立即返回。执行不会等待
服务
停止。你需要在你的设计中考虑到这一点。好的,我理解。非常感谢。