2011-10-24 77 views
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); 
} 

}

我失去了一些地方在我的代码?

回答

0

我想你需要一个broadcastreciever类来了解下次何时触发闹钟。 参考此tutorial: Alarm Notification

and aslo Android的报警管理器不记得设备重新启动时的警报。 为了恢复警报,您需要执行以下步骤:

1.在设置警报的活动中,还将有关每个警报的信息保存到数据库中。

2.创建一个额外的BroadcastReceiver,在启动时调用它来重新安装警报。

想了解更多请参考API Demo's - Alarm

+0

喜阳光充足,感谢您的回复。我有一个广播接收器。但基于SDK中的示例,我注意到setRepeating代码使用ELAPSED_REALTIME_WAKEUP,而我正在使用RTC_WAKEUP。这可能是运行时错误的原因,我得到 – Ashley

+0

仍然需要帮助 - 尝试mAlarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,SystemClock.elapsedRealtime(),AlarmManager.INTERVAL_FIFTEEN_MINUTES,PI);并且它在15分钟后仍然不会触发另一个报警 – Ashley

+0

也试过mAlarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,SystemClock.elapsedRealtime(),AlarmManager.INTERVAL_FIFTEEN_MINUTES,pi);它也不会在15分钟后触发另一个报警 – Ashley