广播接收机仅响应一天

时间:2019-01-30 19:13:55

标签: java android broadcastreceiver broadcast

我想每天收到有关namaz时间的通知。假设5个wakth namaj通知。因此,我制作了一个应用程序,并使用广播接收器在后台工作,并在确切时间到达时向我发送通知。我的应用程序一天可以正常运行。经过一天后,它不再发送通知给我。我在这里附上我的代码

Calendar c1 = Calendar.getInstance();
    Calendar c2 = Calendar.getInstance();
    Calendar c3 = Calendar.getInstance();
    Calendar c4 = Calendar.getInstance();
    Calendar c5 = Calendar.getInstance();
    Calendar c6 = Calendar.getInstance();
    c1.set(c1.get(Calendar.YEAR), c1.get(Calendar.MONTH), c1.get(Calendar.DAY_OF_MONTH), 4, 5, 0);
    c2.set(c2.get(Calendar.YEAR), c2.get(Calendar.MONTH), c2.get(Calendar.DAY_OF_MONTH), 5, 30, 0);
    c3.set(c3.get(Calendar.YEAR), c3.get(Calendar.MONTH), c3.get(Calendar.DAY_OF_MONTH), 12, 21, 0);
    c4.set(c4.get(Calendar.YEAR), c4.get(Calendar.MONTH), c4.get(Calendar.DAY_OF_MONTH), 16, 10, 0);
    c5.set(c5.get(Calendar.YEAR), c5.get(Calendar.MONTH), c5.get(Calendar.DAY_OF_MONTH), 17, 30, 0);
    c6.set(c6.get(Calendar.YEAR), c6.get(Calendar.MONTH), c6.get(Calendar.DAY_OF_MONTH), 19, 10, 0);
    setAlarm(c1.getTimeInMillis(), "Tahajjud");
    setAlarm(c2.getTimeInMillis(), "Fojor");
    setAlarm(c3.getTimeInMillis(), "Juhur");
    setAlarm(c4.getTimeInMillis(), "Asor");
    setAlarm(c5.getTimeInMillis(), "Magrib");
    setAlarm(c6.getTimeInMillis(), "Esha");

我的setAlarm方法是

    String namajText = namaj;
    AlarmManager alarmManager =(AlarmManager) getSystemService(Context.ALARM_SERVICE);

    Intent intent = new Intent(BackgroundNotificationOnTime.this,MyAlarm2.class);

    intent.putExtra("Namaj", namajText);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(this,count,intent,PendingIntent.FLAG_ONE_SHOT);

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,timeInMillis,AlarmManager.INTERVAL_DAY,pendingIntent);

    count++;

那是我的onReceive方法

String namajText = intent.getStringExtra("Namaj");
    int nId = 0;
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("Namaj Alert")
            .setContentText(namajText+" namaj wakth started.")
            .setAutoCancel(true)
            .setDefaults(NotificationCompat.DEFAULT_ALL);

    Uri path = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    builder.setSound(path);

    NotificationManager notificationManager = (NotificationManager) context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(nId,builder.build());

1 个答案:

答案 0 :(得分:0)

您将PendingIntent.FLAG_ONE_SHOT用于PendingIntent,因此每个警报仅触发一次。因此,您需要使用其他值来重复警报,例如0。

documentationPendingIntent.FLAG_ONE_SHOT

  

标记,指示此PendingIntent只能使用一次。与getActivity(Context,int,Intent,int),getBroadcast(Context,int,Intent,int)和getService(Context,int,Intent,int)一起使用。

     

如果设置,则在调用send()后,它将自动为您取消,并且以后尝试通过它发送的任何尝试都会失败。