How to run service in background every 10 minute?

后端 未结 5 1010
独厮守ぢ
独厮守ぢ 2021-02-10 03:04

I want to use a service that run in background indefinitely and call a method every 10 minute and its running even app killed

How to create it?

5条回答
  •  醉酒成梦
    2021-02-10 03:45

    Assuming you have a Running Service

    User AlarmManager to run Service every 10 minutes

       AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
             Intent i = new Intent(context, YourService.class);
             PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
             am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 600000, pi); // Millisec * Second * Minute
         }
    

提交回复
热议问题