gpt4 book ai didi

android - 如何在特定时间在 Android 应用程序中设置通知?

转载 作者:行者123 更新时间:2023-11-29 15:12:41 24 4
gpt4 key购买 nike

我在 android 应用程序 中使用了以下代码来使用 AlarmManager 设置通知(警报)

 public void setAlarm() {
Intent intent = new Intent(MainActivity.this, TimeAlarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);

am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
(40000), pendingIntent);
}

或者我应该使用下面的代码

   Calendar calendar = Calendar.getInstance();

calendar.set(Calendar.MONTH, 3);
calendar.set(Calendar.YEAR, 2015);
calendar.set(Calendar.DAY_OF_MONTH, 21);

calendar.set(Calendar.HOUR_OF_DAY,11);
calendar.set(Calendar.MINUTE, 15);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM,Calendar.AM);

Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0);

AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);

I want to set (notification)alarm at this specific time : 11:15AM on 21-March-2015

Can any one help me to set this?

最佳答案

首先,您需要使用 Calendar 来设置您的日期,例如:

Calendar cal = Calendar.getInstance();
cal.set(Calendar.MONTH, 3);
cal.set(Calendar.YEAR, 2015);
cal.set(Calendar.DAY_OF_MONTH, 15);

cal.set(Calendar.HOUR_OF_DAY, 11);
cal.set(Calendar.MINUTE, 15);

然后在您的 setAlarm() 方法中,您只需设置一次闹钟。你不需要重复它:

public void setAlarm() {
Intent intent = new Intent(MainActivity.this, TimeAlarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);

am.set(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),pendingIntent);
}

关于android - 如何在特定时间在 Android 应用程序中设置通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29059651/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com