I want to make Background Service to add item to RecyclerView every 24 hours

Micheal_ Moris :

I'm trying to start a service that adds an item to RecyclerView every 24 hours (every day). I used this code but it doesn't work:

BackgroundService.java

public class BackgroundService extends Service {
private boolean isRunning;
private Context context;
private Thread backgroundThread;
SharedPreferences pref;
String isMorningChecked;
String isEveningChecked;
String isNightChecked;
String isEchuraistChecked;
String isConfessChecked;
String isBibleChecked;
String Date;
@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();
    this.context = this;
    this.isRunning = false;
    this.backgroundThread = new Thread(addNewNote);
}


private Runnable addNewNote = new Runnable() {
    @Override
    public void run() {
        Log.e("isRunning", "Yeeeeeeeeeees!");
        pref = getApplicationContext().getSharedPreferences("checkStates", 0);
        String checkMorningState = pref.getString("morning", "False");
        String checkEveningState = pref.getString("evening", "False");
        String checkNightState = pref.getString("night", "False");
        String checkEchuraistState = pref.getString("echuraist", "False");
        String checkConfessState = pref.getString("confess", "False");
        String checkBibleState = pref.getString("bible", "False");
        String writeYourNotes = pref.getString("writeNotes", "");
        SimpleDateFormat sdf = new SimpleDateFormat("E", new Locale("ar"));
        final SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy.MM.dd");
        final Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        Date = sdf2.format(timestamp).toString();
        if(checkMorningState.equals("True")){

            isMorningChecked = "+";

        }else{

            isMorningChecked = "-";

        }
        if(checkEveningState.equals("True")){

            isEveningChecked = "+";

        }else{

            isEveningChecked = "-";

        }
        if(checkNightState.equals("True")){

            isNightChecked = "+";

        }else{

            isNightChecked = "-";

        }
        if(checkEchuraistState.equals("True")){

            isEchuraistChecked = "+";

        }else{

            isEchuraistChecked = "-";

        }
        if(checkConfessState.equals("True")){

            isConfessChecked = "+";

        }else{

            isConfessChecked = "-";

        }
        if(checkBibleState.equals("True")){

            isBibleChecked = "+";

        }else{

            isBibleChecked = "-";
        }

        AppDatabase db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "Note").allowMainThreadQueries().build();
        db.userDao().insertAll(new Note(sdf.format(timestamp), sdf2.format(timestamp).toString(), isMorningChecked, isEveningChecked, isNightChecked, isEchuraistChecked, isConfessChecked, isBibleChecked, writeYourNotes));

        stopSelf();
    }
};


@Override
public void onDestroy() {
    super.onDestroy();
    this.isRunning = false;

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    if(!this.isRunning){

        this.isRunning = true;
        this.backgroundThread.start();

    }

    return START_STICKY;

}
}

BoardCastReceiver.java

public class AlarmReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
        context.startService(new Intent(context, BackgroundService.class));
}
}

Home.java

Intent alarm = new Intent(this, AlarmReceiver.class);
    boolean alarmRunning = ((PendingIntent.getBroadcast(this,0,alarm,PendingIntent.FLAG_NO_CREATE)) != null);
    if(alarmRunning == false){

        PendingIntent pendingIntent = PendingIntent.getBroadcast(this,0,alarm,0);
        AlarmManager  alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 1000, pendingIntent);

I have tried this but the Service works once or twice and then it stops and sometimes it gives me the following error:

java.lang.IllegalStateException: Not allowed to start service Intent

Usman Zafer :

Background service can not be used for long running tasks on Android 8.0 onwards. Please use JobScheduler API or Workmanager to achieve the desired results. Consult the following link for details.

https://developer.android.com/topic/libraries/architecture/workmanager

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=217181&siteId=1