Android 每天自动更改文本视图

Android 每天自动更改文本视图,android,textview,Android,Textview,首先,我想每天自动更改文本视图文本。例如,第一天文本视图文本是第一个文本,第二天文本是每天8点的第二个文本。文本将永久保存到第二天8点 我尝试了这个代码,但我的问题是,如果用户在8点钟没有打开这个页面,代码就不会被触发。文本位于mquote数组中 SharedPreferences pref =getApplicationContext().getSharedPreferences("myPrefsKey",Context.MODE_PRIVATE) ; dayCount = pre

首先,我想每天自动更改文本视图文本。例如,第一天文本视图文本是第一个文本,第二天文本是每天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();


}

在首选项中上次更新首选项时存储毫秒数:

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

}

您应该使用时间戳来比较两个日期。试试这个代码。 但这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();
您需要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();
        }
    }
}

检查此项而不是检查准确时间8:01,检查它是否大于某个值。这不起作用,因为您正在检查时间是否为8:01。您应该检查时间是否大于8:00,以及您是否仍在同一天。@zed这很好,但每次单击都会在同一天更改文本。所以新的问题是如何知道这一天是一样的还是不一样的谢谢你们的回答。我认为这是一个很好的算法,但它不起作用。当时间是8点钟的时候,文本不更新,总是第一个文本在那里。听起来很傻,但是,我没有把tvQuote.setTextmquote.getQuotedayCount;在我的回答中,你错过了吗?另外,我将天数传递给UpdateNote方法;我错了吗?另外,每次创建视图时,我们都需要执行天数计算。或者正在被看到。我认为AlarmManager是合适的。你先通知经理。谢谢你的回答@乌古博兹