0

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

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

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

4

2 回答 2

0

问题仅按照@user1140237 的建议设置重复

于 2013-07-16T09:32:58.897 回答
0

尝试

 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>
于 2013-07-16T09:24:48.317 回答