4

Hi and thank you for your help.

I have an application that uses AlarmManager to set one alarm a day for the next weeks, months...

  • I set one alarm for each day of the week to start the Activity

and

  • one alarm for each day of the week for stopping the Activity after some time

I have the following problem that I will try to explain in the following lines:

Today is Wednesday,

I open the application and set my alarms for MON, TUE, WED, THU, FRI, SAT, SUN... as soon as I set the alarms:

ALL the alarms for MON and TUE go immediately off I end up with 4 instances of the Activity !!!!

Please how do I avoid this???

Please see a piece of my code:

        // SET THE ALARM FOR STARTING THE ACTIVITY 
        Intent smon = new Intent(ctxt, VideoActivty.class);
        smon.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent psmon = PendingIntent.getActivity(ctxt, 0, smon, 0);

        Calendar calSet = Calendar.getInstance();
        calSet.set(Calendar.DAY_OF_WEEK, 2);
        calSet.set(Calendar.HOUR_OF_DAY, hsmon);
        calSet.set(Calendar.MINUTE, msmon);
        calSet.set(Calendar.SECOND, 0);
        calSet.set(Calendar.MILLISECOND, 0);

        mgr.setRepeating(AlarmManager.RTC_WAKEUP, calSet.getTimeInMillis(),
                7 * 24 * 60 * 60 * 1000, psmon);

        // SET THE ALARM FOR KILLING THE ACTIVITY 
        Intent fmon = new Intent(ctxt, VideoActivty.class);
        fmon.putExtra("finish", true);
        PendingIntent pfmon = PendingIntent.getActivity(ctxt, 0, fmon, 0);

        calSet.set(Calendar.DAY_OF_WEEK, 2);
        calSet.set(Calendar.HOUR_OF_DAY, hfmon);
        calSet.set(Calendar.MINUTE, mfmon);
        calSet.set(Calendar.SECOND, 0);
        calSet.set(Calendar.MILLISECOND, 0);

        mgr.setRepeating(AlarmManager.RTC_WAKEUP, calSet.getTimeInMillis(),
                7 * 24 * 60 * 60 * 1000, pfmon);

This is the Activity:

public class VideoActivty extends Activity {
private VideoView video;
private MediaController ctlr;
private PowerManager.WakeLock wl;
private KeyguardLock keyguard;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //
    PowerManager pm = (PowerManager) this
            .getSystemService(this.POWER_SERVICE);
    wl = pm.newWakeLock(
            PowerManager.PARTIAL_WAKE_LOCK, "");
    wl.acquire();

    KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
    keyguard = km.newKeyguardLock("MyApp");
    keyguard.disableKeyguard();

    getWindow().setFormat(PixelFormat.TRANSLUCENT);
    VideoView videoHolder = new VideoView(this);
    //if you want the controls to appear
    videoHolder.setMediaController(new MediaController(this));
    Uri video = Uri.parse("android.resource://" + getPackageName() + "/" 
    + R.raw.ingress); //do not add any extension
    //if your file is named sherif.mp4 and placed in /raw
    //use R.raw.sherif
    videoHolder.setVideoURI(video);
    setContentView(videoHolder);

    videoHolder.start();
    videoHolder.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer mp) {

                        mp.start();
        }
    });
}


@Override
protected void onNewIntent (Intent i){


  if( i.getBooleanExtra("finish",false) ){
      wl.release();
      keyguard.reenableKeyguard();
      finish();
  }
}


}
4

4 回答 4

5

正如AlarmManager的 API 页面所述,关于set(...)setRepeating(...)方法:

“如果规定的触发时间是过去,警报将立即触发,警报计数取决于触发时间相对于重复间隔过去多长时间。”

因此,例如,如果您在星期三,则 AlarmManager 将触发以下事件:星期日、星期一、星期二和(可能)星期三。它实际上只会为所有人触发一个。

这是我避免这种情况的方法。不确定这是最好的方法,但它工作顺利。

假设您将要触发 AlarmManager的星期几小时分钟存储在以下变量中:

final int myAlarmDayOfTheWeek = ...;
final int myAlarmHour = ...;
final int myAlarmMinute = ...;

当然,在这里我假设您使用与Calendar 类相同的约定存储星期几,即:

星期日 = 日历.SUNDAY = 1

星期一 = Calendar.MONDAY = 2

…………

星期六 = 日历。星期六 = 7

然后:

Calendar timestamp = Calendar.getInstance();

//Check whether the day of the week was earlier in the week:
if( myAlarmDayOfTheWeek > timestamp.get(Calendar.DAY_OF_WEEK) ) {
    //Set the day of the AlarmManager:
    time.add(Calendar.DAY_OF_YEAR, (myAlarmDayOfTheWeek - timestamp.get(Calendar.DAY_OF_WEEK)));
}
else {
    if( myAlarmDayOfTheWeek < timestamp.get(Calendar.DAY_OF_WEEK) ) {
        //Set the day of the AlarmManager:
        timestamp.add(Calendar.DAY_OF_YEAR, (7 - (timestamp.get(Calendar.DAY_OF_WEEK) - myAlarmDayOfTheWeek)));
    }
    else {  // myAlarmDayOfTheWeek == time.get(Calendar.DAY_OF_WEEK)
        //Check whether the time has already gone:
        if ( (myAlarmHour < timestamp.get(Calendar.HOUR_OF_DAY)) || ((myAlarmHour == timestamp.get(Calendar.HOUR_OF_DAY)) && (myAlarmMinute < timestamp.get(Calendar.MINUTE))) ) {
            //Set the day of the AlarmManager:
            timestamp.add(Calendar.DAY_OF_YEAR, 7);
        }
    }
}

//Set the time of the AlarmManager:
timestamp.set(Calendar.HOUR_OF_DAY, myAlarmHour);
timestamp.set(Calendar.MINUTE, myAlarmMinute);
timestamp.set(Calendar.SECOND, 0);

请注意,通过在Calendar.add(...)中使用Calendar.DAY_OF_YEAR,您甚至不需要处理月份的变化...

如果您处于第一个条件(即 myAlarmDayOfTheWeek > timestamp.get(Calendar.DAY_OF_WEEK)),您也可以只使用Calendar.set(...)Calendar.DAY_OF_WEEK作为字段和myAlarmDayOfTheWeek作为值。

现在,您只需要设置您的 AlarmManager。例如:

final AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, timestamp.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, myAlarmPendingIntent);

请避免每次都计算天间隔(即 24 * 60 * 60 * 1000),而是使用AlarmManager.INTERVAL_DAY(或最多86400000)。

于 2014-01-08T15:28:01.863 回答
4

我们这里有解决方案吗?我仍在寻找解决方案,我的代码如下..

calendar.set(Calendar.DAY_OF_WEEK,getweekday(obj));
    calendar.set(Calendar.HOUR_OF_DAY,timepicker.getCurrentHour());
    calendar.set(Calendar.MINUTE, timepicker.getCurrentMinute());
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    if(calendar.before(cal_now)){//if its in the past increment
        calendar.add(Calendar.DATE,1);
    }

PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(),_id, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY * 7, sender);
于 2013-07-11T14:54:14.413 回答
1

从警报管理器

int RTC_WAKE System.currentTimeMillis() 中的闹钟时间(UTC 挂钟时间),它将在设备关闭时唤醒设备。`

从日历

日历.getInstance()

日历子类实例设置为默认时区中的当前日期和时间。

这两个使用将不同的时区。

此外,仅通过更改DAY_OF_WEEK您不会更改WEEK_OF_YEARCalendar 对象的字段。这意味着如果它是星期三,但您正在为星期一设置警报(通过仅更改星期几),它将立即响起,因为它是为本周的星期一设置的。如果您需要在同一时间为一周中的每一天设置闹钟

    alarmManager(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (24hoursOfMilliseconds), 24hoursOfMilliseconds, pendingIntent);

这将设置相同的警报,每 24 小时重复一次。如果您需要在不同的日子做不同的事情,只需使用

switch(calendarObj.get(Calendar.DAY_OF_WEEK) { }
于 2013-07-10T17:48:52.280 回答
0

只需使用 boolean 以共享偏好注册今天的日期,并在第一次执行时将其设为 true。在重新启动期间,如果通知已经发送,则忽略,否则通知用户。第二天只需删除旧的偏好并创建新的共享偏好值

public boolean isAlreadyWished(String date,String previousDate){
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
    if(sharedPref.contains(alreadyNotifiedString+previousDate)){
        sharedPref.edit().remove(alreadyNotifiedString+previousDate);
    }
    if(sharedPref.contains(alreadyNotifiedString+date)){
        boolean displayOrderString = sharedPref.getBoolean(alreadyNotifiedString+date, false);
        return  displayOrderString;
    }else{
        SharedPreferences.Editor editor=sharedPref.edit();
        editor.putBoolean(alreadyNotifiedString+date,true);
        return  false;
    }

}
于 2015-07-01T12:29:49.563 回答