gpt4 book ai didi

java - 我想让后台服务每 24 小时将项目添加到 RecyclerView

转载 作者:行者123 更新时间:2023-12-01 18:00:26 25 4
gpt4 key购买 nike

我正在尝试启动一项服务,该服务每 24 小时(每天)向 RecyclerView 添加一个项目。我使用了这段代码,但它不起作用:

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);

我已经尝试过此操作,但该服务工作了一两次,然后就停止了,有时会出现以下错误:

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

最佳答案

在 Android 8.0 及以上版本上,后台服务无法用于长时间运行的任务。请使用JobScheduler API或Workmanager来达到预期的结果。请参阅以下链接了解详细信息。

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

关于java - 我想让后台服务每 24 小时将项目添加到 RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60645145/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com