Если вы хотите, чтобы служба продолжала работать в течение длительного периода времени в фоновом режиме, то хорошим началом будет липкая служба .. в основном вы создадите класс, который расширяет класс service
, и вы можете использовать START_STICKY
результат, чтобы держать его в течение длительных периодов времени.
public class MyService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
//this commandresult makes the service a sticky service
return START_STICKY;
}
//you can create a class that runs on a timer or a loop
// inside this service and it will continue running until the OS or user kills it
// create a timer then query your database inside this class and send out
//notifications accordingly
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}
}
Источник: https://www.tutorialspoint.com/android/android_services.htm