0
我正在尝试开发一款应用程序,使我可以在每天的特定时间设置重复报警,例如每天3.15pm。到目前为止,我已经设法让它在状态栏中为下午3.15的第一个实例发出通知,但是它并没有在第二天发出另一个警报。下面是一些我写了如何设置重复报警
设定闹钟
public void setAlert(Long taskId, Calendar when) {
Intent i = new Intent(mContext, OnAlarmReceiver.class);
i.putExtra(AlertsDbAdapter.FLD_ROWID, (long)taskId);
PendingIntent pi = PendingIntent.getBroadcast(mContext, 0, i, PendingIntent.FLAG_ONE_SHOT);
mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);
}
唤醒设备的通知
NotificationManager mgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(this, AlertDisplay.class);
notificationIntent.putExtra(AlertsDbAdapter.FLD_ROWID, rowId);
PendingIntent pi = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT);
Notification note=new Notification(android.R.drawable.stat_sys_warning, "alert", System.currentTimeMillis());
note.setLatestEventInfo(this, "Test Alert", "alert", pi);
note.defaults |= Notification.DEFAULT_SOUND;
note.flags |= Notification.FLAG_AUTO_CANCEL;
int id = (int)((long)rowId);
mgr.notify(id, note);
的广播接收器的代码
public static void acquireStaticLock(Context context) {
getLock(context).acquire();
}
synchronized private static PowerManager.WakeLock getLock(Context context) {
if (lockStatic==null) {
PowerManager mgr=(PowerManager)context.getSystemService(Context.POWER_SERVICE);
lockStatic=mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
LOCK_NAME_STATIC);
lockStatic.setReferenceCounted(true);
}
return(lockStatic);
}
public WakeAlertIntentService(String name) {
super(name);
}
@Override
final protected void onHandleIntent(Intent intent) {
try {
doAlertWork(intent);
}
finally {
getLock(this).release();
}
}
设置代码
public class OnAlarmReceiver extends BroadcastReceiver {
private static final String TAG = ComponentInfo.class.getCanonicalName();
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Received wake up from alarm manager.");
long rowid = intent.getExtras().getLong(AlertsDbAdapter.FLD_ROWID);
WakeAlertIntentService.acquireStaticLock(context);
Intent i = new Intent(context, AlertService.class);
i.putExtra(AlertsDbAdapter.FLD_ROWID, rowid);
context.startService(i);
}
}
我失去了一些地方在我的代码?
喜阳光充足,感谢您的回复。我有一个广播接收器。但基于SDK中的示例,我注意到setRepeating代码使用ELAPSED_REALTIME_WAKEUP,而我正在使用RTC_WAKEUP。这可能是运行时错误的原因,我得到 – Ashley
仍然需要帮助 - 尝试mAlarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,SystemClock.elapsedRealtime(),AlarmManager.INTERVAL_FIFTEEN_MINUTES,PI);并且它在15分钟后仍然不会触发另一个报警 – Ashley
也试过mAlarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,SystemClock.elapsedRealtime(),AlarmManager.INTERVAL_FIFTEEN_MINUTES,pi);它也不会在15分钟后触发另一个报警 – Ashley