0

我正在使用 AlarmManager() 来触发通知。但不知道如何在上午 10 点和每 24 小时点火一次。任何人请帮助我。

我的代码如下:

        Calendar Calendar_Object = Calendar.getInstance();
        Calendar_Object.set(Calendar.HOUR_OF_DAY, 10);
        Calendar_Object.set(Calendar.MINUTE, 00);
        Calendar_Object.set(Calendar.SECOND, 0);
        Intent myIntent = new Intent(Splash.this, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(Splash.this, 0, myIntent, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC,Calendar_Object.getTimeInMillis(),(24 * 60 * 60 * 1000),pendingIntent);
4

2 回答 2

1

每 24 小时尝试以下代码:

SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
long now = System.currentTimeMillis();
long diffMillis = now - pref.getLong("CurrentTimeMillis", 0);
if( diffMillis >= (3600000  * 24) ) {

 // Your code here:)
SharedPreferences.Editor editor = pref.edit();
editor.putLong("CurrentTimeMillis", System.currentTimeMillis());
editor.commit();

} else {
 // too early
}
于 2013-05-31T12:01:43.457 回答
0
 Calendar Calendar_Object = Calendar.getInstance();
        Calendar_Object.set(Calendar.HOUR_OF_DAY, 10);
        Calendar_Object.set(Calendar.MINUTE, 00);
        Calendar_Object.set(Calendar.SECOND, 0);
        Intent myIntent = new Intent(Splash.this, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(Splash.this, 0, myIntent, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC,Calendar_Object.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);
于 2013-05-31T12:03:07.333 回答