我的数据库中有一组提醒(按时间排序)。当我的应用程序启动时,我调用setAlarm
. 我需要在onReceive
方法中添加代码才能完成这些任务:
- 从我的数据库中获取第一个提醒
- 获取与提醒相关的延迟
- 安排一个新的警报以获得下一个提醒。
我创建了一个简单的 BroadcastReceiver 类:
public class AlarmReceiver extends BroadcastReceiver{
private static final String DEBUG_TAG= "AlarmReceiver";
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
Log.d(DEBUG_TAG,"ALARM!!!");
// --mycode--
}
}
和活动类:
public class AlarmActivity extends Activity {
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
context = getApplicationContext();
}
public void setAlarm(View v){
Intent intent = new Intent(this,AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+ Delay,pendingIntent);
Log.i("SETTER","Alarm started");
}
public void stopAlarm(View v){
Intent intent = new Intent(this,AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
pendingIntent.cancel();
}
}
现在,我希望在--mycode--
部分中从数据库中获取新的延迟(如果存在),并使用这个新的延迟设置新的警报。如何从 onReceive 方法设置新的 AlarmManager?