多个警报 SQLite

发布于 2024-12-19 04:18:02 字数 1695 浏览 3 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔嚣张 2024-12-26 04:18:02

幸运的是,答案非常简单。您的代码是:

PendingIntent pi = PendingIntent.getBroadcast(mContext, 0, i, PendingIntent.FLAG_ONE_SHOT);

第二个参数是 request_code 就像 PendingIntent 的 id,因为您总是覆盖以前的 PendingIntent。因此,您要做的就是为您需要的每个警报设置一个唯一的请求代码。在你的情况下,你可以使用reminderId,我猜是数据库行id。因为这是每个警报的唯一 ID,所以非常适合。

PendingIntent pi = PendingIntent.getBroadcast(mContext, reminderId, i, PendingIntent.FLAG_ONE_SHOT);

Luckily the answer is pretty simple. Your code is:

PendingIntent pi = PendingIntent.getBroadcast(mContext, 0, i, PendingIntent.FLAG_ONE_SHOT);

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.

PendingIntent pi = PendingIntent.getBroadcast(mContext, reminderId, i, PendingIntent.FLAG_ONE_SHOT);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文