使用 sqlite 数据库设置多个警报,但只剩下一个警报

Posted

技术标签:

【中文标题】使用 sqlite 数据库设置多个警报,但只剩下一个警报【英文标题】:Set multiple alarms using sqlite database but only one alarm remaining 【发布时间】:2013-07-02 14:56:33 【问题描述】:

我使用 SQLite 设置了多个闹钟,但是当我设置时钟时,只有一个闹钟是我设置的最后一个闹钟。我该如何解决这个问题?

public class alert extends Activity


    DatePicker pickerDate;
    TimePicker pickerTime;
    Button buttonSetAlarm;
    Button insertButton;`enter code here`
    TextView info;

    Context mContext; 
    AlarmManager mAlarmManager;

    dalAlarm dbAlarm ;
    SQLiteOpenHelper dbHelper;
    SQLiteDatabase database;

    final static int RQS_1 = 0;

    dalAlarm dalAl = new dalAlarm(this);


    public void onCreate(Bundle savedInstanceState)
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alertscreen);


        info = (TextView)findViewById(R.id.info);
        pickerDate = (DatePicker)findViewById(R.id.pickerdate);
        pickerTime = (TimePicker)findViewById(R.id.pickertime);

        Calendar now = Calendar.getInstance();
        pickerDate.init(
                now.get(Calendar.YEAR), 
                now.get(Calendar.MONTH), 
                now.get(Calendar.DAY_OF_MONTH), 
                null);

        pickerTime.setCurrentHour(now.get(Calendar.HOUR_OF_DAY));
        pickerTime.setCurrentMinute(now.get(Calendar.MINUTE));

        buttonSetAlarm = (Button)findViewById(R.id.setalarm);
        buttonSetAlarm.setOnClickListener(new OnClickListener()

            @Override
            public void onClick(View arg0) 
                Calendar current = Calendar.getInstance();

                Calendar cal = Calendar.getInstance();
                cal.set(pickerDate.getYear(), 
                        pickerDate.getMonth(), 
                        pickerDate.getDayOfMonth(), 
                        pickerTime.getCurrentHour(), 
                        pickerTime.getCurrentMinute(), 
                        00);

                //dbAlarm.addNewAlarm(new dalHelper_alarm(pickerTime.getCurrentHour(),pickerTime.getCurrentMinute(),cal.getTime().toString()));
                if(cal.compareTo(current) <= 0)

                    Toast.makeText(getApplicationContext(), 
                            "Invalid Date/Time", 
                            Toast.LENGTH_LONG).show();
                else

                    //  dbAlarm.addNewAlarm(new dalHelper_alarm(pickerTime.getCurrentHour(),pickerTime.getCurrentMinute(),cal.getTime().toString()));


                    setAlarm(cal);

                    database();

                

            

        );



    


    public void setAlarm(Calendar targetCal)
    

        info.setText("\n\n***\n"
                + "Alarm is set@ " + targetCal.getTime() + "\n"
                + "***\n");

        dalAlarm db = new dalAlarm(alert.this);

        dalHelper_alarm test = new dalHelper_alarm(pickerTime.getCurrentHour(),pickerTime.getCurrentMinute(),targetCal.getTime().toString());

        db.addNewAlarm(test);





    


    public void newAlarm(Calendar newCal)


        Intent intent = new Intent(getBaseContext(), popUp.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(getBaseContext(), 0, intent, PendingIntent.FLAG_ONE_SHOT);
        AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, newCal.getTimeInMillis(), pendingIntent);  
    

    public void database()


        ArrayList<dalHelper_alarm> alarm = dalAl.getAllAlarm();
        Calendar newCal = Calendar.getInstance();



        newCal.set(pickerDate.getYear(), 
                pickerDate.getMonth(), 
                pickerDate.getDayOfMonth(), 
                alarm.get(0).getHour(), 
                alarm.get(0).getMin(), 
                00);
        info.setText("\n\n*** \n"
                + "Alarm is set@ " + newCal.getTime() + "\n"
                + "***\n");

        newAlarm(newCal);



    

【问题讨论】:

【参考方案1】:

PendingIntent pendingIntent = PendingIntent.getActivity(getBaseContext(), 0, intent, PendingIntent.FLAG_ONE_SHOT);

您使用的是相同的请求代码 (0)。将其更改为每个警报唯一的内容(可能是 YYYYMMDDHHMMSS?)。

【讨论】:

以上是关于使用 sqlite 数据库设置多个警报,但只剩下一个警报的主要内容,如果未能解决你的问题,请参考以下文章

在每天的特定时间设置启动时间警报

带有 sqlite 的核心数据仅返回 3 行

跨多个域使用一个 Jquery 警报

核心数据文件损坏时如何绕过警报消息?

“kotlin”sqlite 查询一条记录

Swift:多个本地通知,但只显示最新的