为什么在设置两个警报时警报会立即开启

时间:2018-03-02 07:03:11

标签: android alarm

我的应用中有两个重复闹钟,每个闹钟都有不同的时间和通知。当用户注册游戏卡时,将调用此警报。

当注册了扑克牌时,这两个闹钟必须每天通知用户。

1。)下午2:30 - 时间超过

2。)下午3:30 - 回归的时间

现在的问题是,每次我设置两个警报时,警报会立即触发,但是当我设置一个警报时,它可以正常工作。

警报的代码

 public void setAlarmRepeating()
{
    player_database = new player_database(MainActivity.this);
    Db_retrieve db = new  Db_retrieve(MainActivity.this);
    db.openDB();
    c = player_database.queryData("select * from player_ID ORDER BY ID DESC");
    try {
        if (c != null && c.moveToNext()) {
            Calendar calendar1=Calendar.getInstance();
            calendar1.set(Calendar.HOUR_OF_DAY,14);
            calendar1.set(Calendar.MINUTE,30);
            calendar1.set(Calendar.MILLISECOND,0);
            Intent intent=new Intent(MainActivity.this,Dog_alarm.class);
            PendingIntent dog1=PendingIntent.getBroadcast(this,1,intent,PendingIntent.FLAG_UPDATE_CURRENT);
            AlarmManager alarm1=(AlarmManager)getSystemService(ALARM_SERVICE);
            alarm1.setInexactRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),calendar1.getTimeInMillis(),dog1);


            Calendar calendar2=Calendar.getInstance();
            calendar2.set(Calendar.HOUR_OF_DAY,15;
            calendar2.set(Calendar.MINUTE,30);
            calendar2.set(Calendar.MILLISECOND,0);
            Intent intent1=new Intent(MainActivity.this,Dog_alarm.class);
            PendingIntent dog2=PendingIntent.getBroadcast(this,2,intent1,PendingIntent.FLAG_UPDATE_CURRENT);
            AlarmManager alarm2=(AlarmManager)getSystemService(ALARM_SERVICE);
            alarm2.setInexactRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),calendar2.getTimeInMillis(),dog2);
            }
            else
             {
            Toast.makeText(MainActivity.this,"No pets registered",Toast.LENGTH_SHORT).show();
            }


        } catch (SQLException e) {
        e.printStackTrace();
    }
}

然后在我的BroadcastReceiver上我做了这个

public void onReceive(Context context, Intent intent) {
    NotificationManager notificationManager1=(NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);
    Intent dog_intent1=new Intent(context,HomeActivity.class);
    PendingIntent dog_pending1=PendingIntent.getActivity(context, 1,dog_intent1, PendingIntent.FLAG_UPDATE_CURRENT);
    Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder builder1=new NotificationCompat.Builder(context)
            .setContentIntent(dog_pending1)
            .setSmallIcon(R.drawable.ic_action_prof_breed_icon)
            .setSound(uri)
            .setContentTitle("Pentra")
            .setContentText("Time to exceed")
            .setAutoCancel(true);
    notificationManager1.notify(1,builder1.build());

    NotificationManager notificationManager2=(NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);
    Intent dog_intent2=new Intent(context,HomeActivity.class);
    PendingIntent dog_pending2=PendingIntent.getActivity(context, 2,dog_intent2, PendingIntent.FLAG_UPDATE_CURRENT);
    Uri uri1= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder builder2=new NotificationCompat.Builder(context)
            .setContentIntent(dog_pending2)
            .setSmallIcon(R.drawable.ic_action_prof_breed_icon)
            .setSound(uri1)
            .setContentTitle("Pentra")
            .setContentText("Time to go back?")
            .setAutoCancel(true);
    notificationManager2.notify(2,builder2.build());
}

如果你能查一查,请告诉我我做错了什么,现在一直在思考这个问题,但仍然不知道出了什么问题。

修改

我尝试添加此项以检查当天是否通过

  if(calendar1.before(Calendar.getInstance())) {
                calendar1.add(Calendar.DATE, 1);
            }

但警报仍然会开火

0 个答案:

没有答案