Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

error of recursive handler within Android service class?

I implemented Handler within the service class. This handler is a recursive form of calling yourself back when you're done.

What I'm curious about is what kind of error this method causes in the Android system. Currently, I don't know what kind of influence this way has. I'd appreciate it if you could look at the code below and answer what kind of error it causes and how to solve the error. (Please tell me the wrong part!)

public class recursiveService extends Service {
 ...
 @Override
   public int onStartCommand(Intent intent, int flags, int startId) {
        m_Handler.sendEmptyMessage(0);
        ...
        startForeground(1234, Notification());
        return START_STICKY;
    }
 @Override
   public void onDestroy() {
        super.onDestroy();
        ...
        stopForeground(true);
        m_Handler.removeCallbacksAndMessages(null);
        m_Handler.removeMessages(0);
    }

 private Handler m_Handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            // work
            // ...
            // recursive handler
            m_Handler.removeCallbacksAndMessages(null);
            m_Handler.removeMessages(0);
            m_Handler.sendEmptyMessageDelayed(0, 2000);
        }
    };
}
question from:https://stackoverflow.com/questions/65881793/error-of-recursive-handler-within-android-service-class

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...