问题描述
我正在开发Android应用程序,在这里我设置每周警报。报警被相应地设置,一切工作正常。
当报警时间小于当前时间,报警器立即触发。因此,为了避免这种立即触发我已经取消了它作为
I am developing an android app, where I am setting an alarm on weekly basis. The alarm gets set accordingly and everything works fine.When the alarm time is less than current time,the alarm was triggering immediately. So to avoid this immediate triggering I have canceled it as
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);
但如果这样做,警报没有得到设定的取消得到当前时间的下一个week.Instead,它得到取消,甚至明年week.What我应该做的正确设置报警下周?
请Help.Thanks!
But if do so, The alarm is not getting set to next week.Instead of getting canceled for current time, Its getting canceled even for next week.What should I do to set the alarm properly to next week?Please Help.Thanks!
推荐答案
尝试
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
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中
添加...
AndroidManifest.xmladd ...
<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>
这篇关于设置每周警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!