Service that runs every minute

前端 未结 3 512
借酒劲吻你
借酒劲吻你 2020-12-24 14:33

I have a service that I am wanting to execute a task every minute in the background. It does not need to execute the task whenever the phone is asleep, only when the user is

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-24 14:44

    Do it like this

        private void ping() {
        try {
            //Your code here or call a method
        } catch (Exception e) {
            Log.e("Error", "In onStartCommand");
            e.printStackTrace();
        }
          scheduleNext();
        }
    
        private void scheduleNext() {
          mHandler.postDelayed(new Runnable() {
            public void run() { ping(); }
          }, 60000);
        }
    
        public int onStartCommand(Intent intent, int x, int y) {
          mHandler = new android.os.Handler();
          ping();
          return START_STICKY;
        }
    

提交回复
热议问题