【问题标题】:I select random days for notification but notification is shown everyday我选择随机日期进行通知,但每天都会显示通知
【发布时间】:2020-08-01 17:02:52
【问题描述】:

我正在尝试使用 android 级别 26+ 的 android studio 设置通知,但如果我选择随机日期进行通知,但每天都会显示通知

如何设置 Calendar.DAYS_OF_WEEK .....就像今天是星期天并且我设置 Calendar.DAYS_OF_WEEK,3(Tuesday) 警报管理器应该只在每个星期二通知,但它也会在星期日显示通知

 private void setNotification(AlarmList values) {
    Cursor data = mDatabaseHelper.getItemID(values.getMedicineName());
    int itemID = -1;
    while (data.moveToNext()) {
        itemID = data.getInt(0);
    }
    if (itemID > -1) {

        Intent intent = new Intent(getApplicationContext(), NotificationReceiver.class);
        intent.putExtra("name", values.getMedicineName());
        intent.putExtra("quantity", values.getQuantity());
        intent.putExtra("quality", values.getQuality());
        intent.putExtra("id", itemID);
        intent.putExtra("days", days);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), (itemID), intent, PendingIntent.FLAG_UPDATE_CURRENT);

        for (int i = 0; i < 7; i++) {
            if (days[i]) {
                Calendar calendar = Calendar.getInstance();

                calendar.set(Calendar.HOUR_OF_DAY, hour);
                calendar.set(Calendar.MINUTE, minute);
                calendar.set(Calendar.SECOND, 0);
                calendar.set(Calendar.MILLISECOND, 0);
                calendar.set(Calendar.DAY_OF_WEEK, (i + 1));

                long alarm_time = calendar.getTimeInMillis();
                
                AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarm_time,
                        AlarmManager.INTERVAL_DAY*7, pendingIntent);
            }
        }
        finish();
        Intent activity = new Intent(AddMedicine.this, TabLayoutActivity.class);
        // i.putExtra("fragment id",1);
        startActivity(activity);
    }

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

public class NotificationReceiver extends BroadcastReceiver {

   @Override
   public void onReceive(Context context, Intent intent) {

       String name = intent.getStringExtra("name");
       String quantity = intent.getStringExtra("quantity");
       String quality = intent.getStringExtra("quality");
       int id = intent.getIntExtra("id", 0);
       boolean days[] = intent.getBooleanArrayExtra("days");

       String Heading = "Medicine Reminder";
       String text = "Reminder for " + name + " " + quantity + " " + quality;
     
               NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
               Intent repeatingIntent = new Intent(context, NotificationView.class);
               repeatingIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

               PendingIntent pendingIntent = PendingIntent.getActivity(context, id, repeatingIntent, PendingIntent.FLAG_UPDATE_CURRENT);

               NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "notify")
                       .setContentIntent(pendingIntent)
                       .setSmallIcon(R.drawable.clock)
                       .setContentTitle(Heading)
                       .setContentText(text)
                       .setAutoCancel(true)
                       .setPriority(NotificationCompat.PRIORITY_DEFAULT);
                       notificationManager.notify(id, builder.build());
                  }
               }

【问题讨论】:

    标签: android notifications broadcastreceiver alarmmanager android-pendingintent


    【解决方案1】:

    以这种方式设置Calendar 是不可靠的。您永远不应该使用DAY_OF_WEEK 调用set()。

    如果您希望在下周三触发警报,您​​应该确定下周三的日期(月、日、年)并将其设置在Calendar。

    见Setting DAY_OF_WEEK returns unexpected result

    另见https://bugs.java.com/bugdatabase/view_bug.do?bug_id=4655637


    如何为以下星期三的当前时间创建Calendar 实例的示例:

    Calendar calendar = Calendar.getInstance();
    int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
    // dayOfWeek is 1..7 (SUNDAY to SATURDAY)
    int daysToAdd = Calendar.WEDNESDAY - dayOfWeek;
    // Subtract current day of week from the day of week you want
    if (daysToAdd < 0) {
        // Current day of the week is after WEDNESDAY, so we go to next week
        daysToAdd += 7;
    }
    // If daysToAdd is zero, today is already WEDNESDAY!
    if (daysToAdd != 0) {
        calendar.add(Calendar.DATE, daysToAdd);
    }
    

    【讨论】:

    • 所以如果我想在每个星期三重复警报,我必须获取多少个日期......如果警报被触发一年,它会像 52 个日期一样大惊小怪......先生还有其他建议吗?
    • 没问题。确定下周三的日期,然后您可以将重复闹钟设置为此后每 7 天重复一次。
    • 你能告诉我如何从 day_of_week 获取日期....就像我想要即将到来的星期三的日期我将如何获得日期.....
    • 我在答案中添加了一个代码 sn-p。我实际上并没有编译代码,因此其中可能存在拼写错误或其他错误。希望你能明白。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多