Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.3k views
Welcome To Ask or Share your Answers For Others

1 Answer

Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...