首先,我想每天自动更改textview文本。例如,第一天文本视图文本是“第一文本”,第二天文本是8点钟的“第二文本”。该文本是永久性的,直到第二天8点。
我尝试使用此代码,但问题是如果用户未在8点打开此页面,则不会触发代码。文本在“mquote数组”里面
SharedPreferences pref =getApplicationContext().getSharedPreferences("myPrefsKey",Context.MODE_PRIVATE) ;
dayCount = pref.getInt("dayCount", 0);
Calendar cal = Calendar.getInstance();
int hour=cal.get(Calendar.HOUR_OF_DAY);
int minute=cal.get(Calendar.MINUTE);
if ((hour==8 && minute==1 ))
updateQuote();
tvQuote.setText(mquote.getQuote(dayCount));
public void updateQuote()
{
if (dayCount==4)
dayCount=0;
dayCount++;
pref.edit().putInt("dayCount",dayCount).apply();
}
答案 0 :(得分:1)
您需要AlarmManager
实施:
public class AlarmReceiver extends BroadcastReceiver {
// The app's AlarmManager, which provides access to the system alarm services.
private AlarmManager alarmMgr;
// The pending intent that is triggered when the alarm fires.
private PendingIntent alarmIntent;
public void setAlarm(Context context) {
alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
// set the Alarm's trigger time to 8:00 a.m.
calendar.set(Calendar.HOUR_OF_DAT, 9);
// Set the alarm to fire at approximately 8:30 a.m., according to the device's
// clock, and to repeat once a day.
alarmManager.setInexactRepeating(AlarmManager.RTC,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent);
}
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
{
alarm.setAlarm(context);
} else if(Objects.equals(intent, alarmIntent)) {
SharedPreferences pref = getApplicationContext().getSharedPreferences("key", Context.MODE_PRIVATE);
int dayCount = pref.getInt("dayCount", 0);
dayCount %= 4;
pref.edit().putInt("dayCount", ++daysCount).apply();
}
}
}
答案 1 :(得分:0)
您在偏好设置中上次更新偏好设置时存储的毫秒数:
SharedPreferences pref = getApplicationContext().getSharedPreferences("msKey", Context.MODE_PRIVATE);
msCount = pref.getInt("msCount", 0);
long milliseconds = System.currentTimeMillis();
long diff = milliseconds - msCount;
diff += (8 * 60 * 60 * 1000); // compensating difference for 8'O clock condition
int days = (int) Math.ceil(diff / 24 / 60 / 60 / 1000);
days %= 4; // Because you were resetting days count after 4 increments
updateQuote(days);
public void updateQuote(days) {
if(days == 0) {
pref.edit().putInt("msCount", milliseconds).apply();
}
}
答案 2 :(得分:0)
您应该使用时间戳来比较两个日期。试试这段代码。 但是在用户使用应用程序时发生了1天,它不会触发。如果你想克服这一点,那么每天使用AlarmManager触发一个事件。
SharedPreferences pref = getApplicationContext().getSharedPreferences("myPrefsKey",Context.MODE_PRIVATE);
lastDay_ms= pref.getInt("time_ms", 0);
Calendar today = Calendar.getInstance();
long diff = today.getTimeInMillis() - lastDay_ms; //result in millis
final long ONE_DAY = 24*60*60*1000; //millis in a day
if (diff > ONE_DAY)
updateQuote();