Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k 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 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...