我正在开发一个Android应用程序,我每周都会设置一个警报。警报相应地设置,一切正常。 当闹钟时间小于当前时间时,闹钟立即触发。所以为了避免这种立即触发,我已将其取消为
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);
但如果这样做,警报就不会被设置到下周。而不是取消当前时间,即使下周也会被取消。我应该怎样做才能将警报正确地设置到下周? 请帮助。谢谢!
答案 0 :(得分: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>
答案 1 :(得分:0)
仅按照@ user1140237
的建议发布重复设置