上午10点和每24小时一次消防通知

时间:2013-05-31 11:46:09

标签: android alarmmanager

我正在使用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);

2 个答案:

答案 0 :(得分: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
}

答案 1 :(得分: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);