【问题标题】:setting an alarm on weekly basis每周设置闹钟
【发布时间】:2013-07-16 20:32:57
【问题描述】:

我正在开发一个安卓应用程序,我每周都会设置一个闹钟。警报会相应设置,一切正常。 当报警时间小于当前时间时,立即触发报警。因此,为了避免这种立即触发,我将其取消为

 Intent intent = new Intent(AlarmClock.this, TaskRecieverForAlarm.class);
 intent.putExtra("AlarmDate", alarmdate);
 intent.putExtra("key", key);
 PendingIntent sender = PendingIntent.getBroadcast(AlarmClock.this, key , intent, 0);

if(AlarmHrsInInt < currentHrs)
 {
   am.cancel(sender);
 }

am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 7*1440*60000 ,sender);

但如果这样做,闹钟不会设置为下周。不是在当前时间取消,而是在下周取消。我应该怎么做才能将闹钟正确设置为下周? 请帮忙。谢谢!

【问题讨论】:

    标签: android alarmmanager android-alarms


    【解决方案1】:

    仅按照@user1140237 的建议发出 setrepeating

    【讨论】:

    • 不要检查时间......我的意思是如果条件。将当前时间转换为毫秒,然后与以毫秒为单位的警报调用时间进行比较
    • 您能指导我将当前时间转换为毫秒吗?
    • 再次检查代码,警报设置为第二天而不是7天之后。尝试将您的日期和时间更改一天并再次测试。 @user1140237 给出的代码应该可以工作。
    【解决方案2】:

    试试

     public static final long DAY_IN_MILLISEC = 1000 * 60 * 60 * 24;
    
    public void initAlarm(Context context) {
        AlarmManager am = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent(context, ReceiverClass.class);
        i.setAction(ReceiverClass.ALARM_INVOKE_ACTION);
    
        // /////Alarm Time
        Calendar alarmCalendar = Calendar.getInstance();
        alarmCalendar.set(Calendar.HOUR_OF_DAY, 20);
        alarmCalendar.set(Calendar.MINUTE, 40);
        alarmCalendar.set(Calendar.SECOND, 0);
        long time = alarmCalendar.getTimeInMillis();
        Calendar cal = Calendar.getInstance();
    
        if (time <= cal.getTimeInMillis())
            time = cal.getTimeInMillis() + DAY_IN_MILLISEC;// /Will set for the
                                                            // next day
        PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
        am.setRepeating(AlarmManager.RTC_WAKEUP, time, DAY_IN_MILLISEC, pi); // MilliSec
                                                                                // *
                                                                                // Sec
                                                                                // *
                                                                                // Mnt
                                                                                // *
                                                                                // Hour
        // /***OR
        // am.setRepeating(AlarmManager.RTC_WAKEUP, time,
        // AlarmManager.INTERVAL_DAY * 1, pi);
    }
    

    ReceiverClass.java

        public class ReceiverClass extends BroadcastReceiver {
        public static final String ALARM_INVOKE_ACTION = "com.test.alarm";
    
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            Log.e("action", "Action");
            if (action.equalsIgnoreCase(ALARM_INVOKE_ACTION)) {
                Log.e("Alarm Invoke", "YES");
            } else if (action.equalsIgnoreCase(Intent.ACTION_TIME_CHANGED)
                    || action.equalsIgnoreCase(Intent.ACTION_TIME_TICK)
                    || action.equalsIgnoreCase(Intent.ACTION_TIMEZONE_CHANGED)
                    || action.equalsIgnoreCase(Intent.ACTION_DATE_CHANGED)
                    || action.equalsIgnoreCase(Intent.ACTION_REBOOT)) {
                Log.e("RESET ALARM HERE.SET AGAIN", "YES");
            }
    
        }
    }
    

    AndroidManifest.xml 添加...

    <receiver android:name="com.hiddenbrains.ui.screens.ReceiverClass" >
                <intent-filter>
                    <action android:name="com.test.alarm" />
                    <action android:name="android.intent.action.DATE_CHANGED" />                
                    <action android:name="android.intent.action.TIME_SET" />
                    <action android:name="android.intent.action.TIMEZONE_CHANGED" />
                    <action android:name="android.intent.action.TIME_TICK" />
                    <action android:name="android.intent.action.REBOOT" />
                </intent-filter>
            </receiver>
    

    【讨论】:

    • 我试过这段代码。它工作正常。但第一次,我尝试设置小于当前时间的闹钟时间,它再次立即触发。此外,如果我设置的警报时间小于当前时间。没有警报触发。所有警报都正确设置为下周。如何避免第一次立即触发?
    • i gve u for day repeat alarm.For week repeatweek 你需要给时间毫秒 7 天 ... public static final long WEEK_IN_MILLISEC = 1000 * 60 * 60 * 24 * 7;
    • 是的,我已经尝试使用 7*1440*60000 即 1000 * 60 * 60 * 24 * 7 代替上述代码的 DAY_IN_MILLISEC。即使那样,它也没有设置为下周。
    • 您还需要在 TIMEZONE_CHANGED 和 TIME_CHANGED ACTION 上重置闹钟 .... ...我想测试你从设置中更改时间。所以如果你更改时间、时区等,你需要再次设置闹钟
    猜你喜欢
    • 2013-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多