多个警报 SQLite
我是 Android 编程新手,我正在开发 Android 应用程序,但我一直在创建多个警报。 我的代码只触发最后一个警报并忽略之前的警报 日期存储在用户指定的数据库(SQlite)中,
这是我的提醒管理器类
public class ReminderManager {
private Context mContext;
private AlarmManager mAlarmManager;
public Appointments_ReminderManager(Context context) {
mContext = context;
mAlarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
}
public void setReminder(Long reminderId, Calendar when) {
Intent i = new Intent(mContext, Appointments_OnAlarmReceiver.class);
i.putExtra(RemindersDbAdapter.KEY_ROWID_APPOIN, (long)reminderId);
PendingIntent pi = PendingIntent.getBroadcast(mContext, 0, i, PendingIntent.FLAG_ONE_SHOT);
mAlarmManager.set(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), pi);
}
}
,这是接收器类,
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ComponentInfo;
import android.util.Log;
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(RemindersDbAdapter.KEY_ROWID_APPOIN);
WakeReminderIntentService.acquireStaticLock(context);
Intent i = new Intent(context, ReminderService.class);
i.putExtra(RemindersDbAdapter.KEY_ROWID_APPOIN, rowid);
context.startService(i);
}
}
有人可以帮助我修改我的代码以触发存储在数据库中的用户指定的多个警报。
I'm new on android programming and i'm working on android application and i'm stuck in creating multiple alarms.
my code only fires the last alarm and ignore the previous ones
the dates are stored in a database (SQlite) that the user specifies
this is my reminder manager class
public class ReminderManager {
private Context mContext;
private AlarmManager mAlarmManager;
public Appointments_ReminderManager(Context context) {
mContext = context;
mAlarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
}
public void setReminder(Long reminderId, Calendar when) {
Intent i = new Intent(mContext, Appointments_OnAlarmReceiver.class);
i.putExtra(RemindersDbAdapter.KEY_ROWID_APPOIN, (long)reminderId);
PendingIntent pi = PendingIntent.getBroadcast(mContext, 0, i, PendingIntent.FLAG_ONE_SHOT);
mAlarmManager.set(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), pi);
}
}
and this is the receiver class
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ComponentInfo;
import android.util.Log;
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(RemindersDbAdapter.KEY_ROWID_APPOIN);
WakeReminderIntentService.acquireStaticLock(context);
Intent i = new Intent(context, ReminderService.class);
i.putExtra(RemindersDbAdapter.KEY_ROWID_APPOIN, rowid);
context.startService(i);
}
}
can someone please help me modifying my code to fire multiple alarms specified by the user that is stored in a database.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
幸运的是,答案非常简单。您的代码是:
第二个参数是 request_code 就像 PendingIntent 的 id,因为您总是覆盖以前的 PendingIntent。因此,您要做的就是为您需要的每个警报设置一个唯一的请求代码。在你的情况下,你可以使用reminderId,我猜是数据库行id。因为这是每个警报的唯一 ID,所以非常适合。
Luckily the answer is pretty simple. Your code is:
The second parameter is the request_code is like an id for the PendingIntent and since you're always override the previous PendingIntent. So what you have to do is to set a unique request code for every alarm you need. In your case you can use the reminderId, which is, I guess the DB row id. And because this is a unique ID for every Alarm it perfectly fits.