当前位置: 首页 > 知识库问答 >
问题:

如何在android studio中设置每天不同时间的每日重复通知?

令狐钧
2023-03-14

我现在有每天重复的通知,每天下午6点重复。我想做的是不是6pm,而是在事件即将开始时显示通知。我有一个events arraylist,它包含日期和时间,这是可能的。下面是我当前显示通知的方式。

这是我主要的activity

Calendar calendar = Calendar.getInstance();
 calendar.set(Calendar.HOUR_OF_DAY, 18);
 calendar.set(Calendar.MINUTE, 30);
 calendar.set(Calendar.SECOND, 0);
 Intent intent1 = new Intent(MainActivity.this, AlarmReceiver.class);
 PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0,intent1, PendingIntent.FLAG_UPDATE_CURRENT);
 AlarmManager am = (AlarmManager) MainActivity.this.getSystemService(MainActivity.this.ALARM_SERVICE);
 am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

这是在我的broadcast_reciever课上

公共类AlarmReceiver扩展了

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        long when = System.currentTimeMillis();
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);

        Intent notificationIntent = new Intent(context, EVentsPerform.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);


        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
                context).setSmallIcon(R.drawable.applogo)
                .setContentTitle("Alarm Fired")
                .setContentText("Events To be PErformed").setSound(alarmSound)
                .setAutoCancel(true).setWhen(when)
                .setContentIntent(pendingIntent)
                .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
        notificationManager.notify(MID, mNotifyBuilder.build());
        MID++;

    }

}

我不确定如何设置时间,所以它会重复什么时候和如果有一个事件。感谢您的任何指点和帮助。谢谢

共有1个答案

潘佐
2023-03-14

您可以为每个事件设置一个警报。读取数据库中的所有事件时间,然后为这些事件设置单独的警报。不要使用setRepeating。设定的报警变种是为不同的安卓版本将手机从睡眠中唤醒。

private void setAlarm(Long dateInMillis) {

    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent alarmIntent = new Intent(this, AlarmReceiver.class);
    PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    if (Build.VERSION.SDK_INT >= 23) {
        am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, dateInMillis, pi);
    } else if (Build.VERSION.SDK_INT >= 19) {
        am.setExact(AlarmManager.RTC_WAKEUP, dateInMillis, pi);
    } else {
        am.set(AlarmManager.RTC_WAKEUP, dateInMillis, pi);
    }
}

DateInmilis是Calender.GetInstance().GetTimeInmilis()用于您要设置警报的时间。所以我希望你有一个长存储在你数据库中,这是使你的生活更容易的价值

public ArrayList<String> getAlarms() {
    SQLiteDatabase db;
    ArrayList<String> result = new ArrayList<String>();
    String myPath = DATABASE_PATH + DATABASE_NAME;

    db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
    selectQuery = "SELECT * FROM TABLE";
    Cursor cursor = db.rawQuery(selectQuery, null);

    if (cursor.moveToFirst()) {
        result.add(cursor.getString(cursor.getColumnIndex("WhateverYourColumnIs")));
        while (cursor.moveToNext()) {
            result.add(cursor.getString(cursor.getColumnIndex("WhateverYourColumnIs")));
        }
    }
    cursor.close();
    db.close();

    return result;
}

您可以将字符串转换为一个长的

DatabaseHelper db = new DatabaseHelper();
ArrayList<String> alarms = db.getAlarms()

for(String alarm : alarms){
    try{
        setAlarm(Long.parseLong(alarm));
    }catch(NumberFormatException nfe){
        //Not a number
    }
}

差不多吧

private Long calendarMillis(String date, String time){
    Long result;
    //Do A bunch of stuff to get the information to fill in below from what you bring in.
    Calendar c = Calendar.getInstance();
    c.set(Calendar.YEAR, year);
    //...Calendar.HOUR, Calendar.MONTH, Calendar.DAY
    c.set(Calendar.MINUTE, minute);
    result = c.getTimeInMillis();
    return result;
}

public class NotificationInformation{
    Long alarmTime;
    String name;
    public void NotificationInformation(){

    }
    public void NotificationInformation(NotificationInformation ni){
        this.name = ni.getName();
        this.alarmTime = ni.getAlarmTime();
    }
    //getters and setters
    public void setAlarmTime(Long alarmTime){
        this.alarmTime = alarmTime;
    }
    public void setName(String name){
        this.nime = nime;
    }
    public Long getAlarmTime(){
        return alarmTime;
    }
    public String getName(){
        return name;
    }
}
 类似资料:
  • 问题内容: 我是iOS开发的新手,但是已经创建了该应用程序,并且我试图在设定的时间创建每日通知。当前,通知针对给定的日期/时间执行一次。我不确定如何使用repeatInterval方法每天进行调度。每天重复通知的最佳方法是什么?任何帮助将不胜感激(是)。 问题答案: 您必须提供NSCalendarUnit值(例如“ HourCalendarUnit”或“ DayCalendarUnit”)以重复通

  • 问题内容: 假设和。我正在寻找最好的方法来重复精确的时间,然后以的形式放置一个扁平数组。 我想尽可能快地执行此操作,因为我必须执行多次。我正在使用Python和numpy,并且数组定义为numpy.ndarray。我周围中搜索和了解,并能很好地被用来重复每个元素次,但我想每个人重复不同的时间。 一种方法是: 我想知道是否有更好的方法。 问题答案: 这就是它的作用: 在纯Python中,您可以执行以

  • 我想开发day应用程序项目的报价。出于这个原因,我想在文本视图中显示每天不同的引用(格言)。报价将设置为24小时,并自动更改00:00:01。今天使用应用程序的人将看到第一个引号,第二天,当他们打开应用程序时,他们将看到第二个引号。第三天不同,第四天不同。。 但是我的代码没有显示不同的报价。每天显示第一天的报价。 下面是我的密码。 例如:day 1=to be or not to be. day

  • 我想让用户能够选择他们将收到通知的时间。 应能够选择一个或多个小时值(0-24),然后每天在选定的小时收到通知。 最好的建模方法是什么? 正在考虑这个解决方案:在用户表中添加一个包含小时的数组列,例如[1、6、23],但不知道在每小时扫描表以查找要向其发送通知的用户时,它的速度会有多快。

  • 这是我的问题:我有通知列表,希望在不同的时间显示它们,但它们同时显示在一起。 第一次调用updateLabel函数: 私有空updateLabel(ArrayList通知实体){ 然后创建通知: 私人通知add NotificationEntity(NotificationEntity){ 然后安排通知: 私有void scheduleNotification(int notificationId

  • 问题内容: UILocalNotification已弃用,因此我想将代码更新为UserNotification框架: 等待初始通知后,如何通过UserNotification框架设置类似的每日或每小时重复? 问题答案: 似乎不支持此操作,但可以使用以下解决方法: 与didReceive(_:withContentHandler :) 结合使用,您可以使用: 我知道这不是最佳方法,但它应该在不使用不