gpt4 book ai didi

android - 在 android 中使用 Pending Intent 发出警报

转载 作者:行者123 更新时间:2023-11-29 15:44:14 25 4
gpt4 key购买 nike

我正在按照教程构建一个 Android 应用程序,该应用程序允许用户将时间(小时一分钟) 存储到他们的本地 sqlite 数据库中。此存储的时间与日历时间进行核对,然后使用 pendingIntent 在时间等于当前时间时响起警报。

下面是一些完成工作的主要代码

public class AlarmManagerHelper extends BroadcastReceiver {

public static final String ID = "id";
public static final String NAME = "name";
public static final String TIME_HOUR = "timeHour";
public static final String TIME_MINUTE = "timeMinute";
public static final String TONE = "alarmTone";
public static final String ACTION_ALARM_CHANGED = "android.intent.action.ALARM_CHANGED";

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

public static void setAlarms(Context context) {
cancelAlarms(context);

AlarmDBHelper dbHelper = new AlarmDBHelper(context);

List<AlarmModel> alarms = dbHelper.getAlarms();

if (alarms != null) {
for (AlarmModel alarm : alarms) {
if (alarm.isEnabled) {

PendingIntent pIntent = createPendingIntent(context, alarm);

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, alarm.timeHour);
calendar.set(Calendar.MINUTE, alarm.timeMinute);
calendar.set(Calendar.SECOND, 00);

//Find next time to set
final int nowDay = Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
final int nowHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
final int nowMinute = Calendar.getInstance().get(Calendar.MINUTE);
boolean alarmSet = false;

//First check if it's later in the week
for (int dayOfWeek = Calendar.SUNDAY; dayOfWeek <= Calendar.SATURDAY; ++dayOfWeek) {
if (dayOfWeek >= nowDay &&
!(dayOfWeek == nowDay && alarm.timeHour < nowHour) &&
!(dayOfWeek == nowDay && alarm.timeHour == nowHour && alarm.timeMinute <= nowMinute)) {
calendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);
setAlarm(context, calendar, pIntent);
alarmSet = true;
setStatusBarIcon(context, alarmSet);
break;
}
}
//Else check if it's earlier in the week
if (!alarmSet) {
for (int dayOfWeek = Calendar.SUNDAY; dayOfWeek <= Calendar.SATURDAY; ++dayOfWeek) {
if ( dayOfWeek <= nowDay && alarm.repeatWeekly) {
calendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);
calendar.add(Calendar.WEEK_OF_YEAR, 1);

setAlarm(context, calendar, pIntent);
alarmSet = true;
break;
}
}
}
}
}
}
}

@SuppressLint("NewApi")
private static void setAlarm(Context context, Calendar calendar, PendingIntent pIntent) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pIntent);
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pIntent);
}
}

public static void cancelAlarms(Context context) {
AlarmDBHelper dbHelper = new AlarmDBHelper(context);

List<AlarmModel> alarms = dbHelper.getAlarms();

if (alarms != null) {
for (AlarmModel alarm : alarms) {
if (alarm.isEnabled) {
PendingIntent pIntent = createPendingIntent(context, alarm);

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pIntent);
}
}
}
}

public static void setStatusBarIcon(Context context, boolean enabled) {

Intent alarmChanged = new Intent(ACTION_ALARM_CHANGED);
alarmChanged.putExtra("alarmSet", enabled);
context.sendBroadcast(alarmChanged);

}

private static PendingIntent createPendingIntent(Context context, AlarmModel model) {
Intent intent = new Intent(context, AlarmService.class);
intent.putExtra(ID, model.id);
intent.putExtra(NAME, model.name);
intent.putExtra(TIME_HOUR, model.timeHour);
intent.putExtra(TIME_MINUTE, model.timeMinute);
intent.putExtra(TONE, model.alarmTone.toString());

return PendingIntent.getService(context, (int) model.id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
}

现在使用上述相同的知识,我希望能够在用户插入数据库的日期时间(即年、月、日、小时和分钟)到期时发出警报。有了这个,我在数据库中添加了年、月、日列。

现在的问题是在日期时间到期时让警报响起的条件。

这是教程在时间到了时发出警报的方式

 for (int dayOfWeek = Calendar.SUNDAY; dayOfWeek <= Calendar.SATURDAY; ++dayOfWeek) {
if (dayOfWeek >= nowDay &&
!(dayOfWeek == nowDay && alarm.timeHour < nowHour) &&
!(dayOfWeek == nowDay && alarm.timeHour == nowHour && alarm.timeMinute <= nowMinute)) {
calendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);
setAlarm(context, calendar, pIntent);
alarmSet = true;
setStatusBarIcon(context, alarmSet);
break;
}
}

这就是我尝试在以下情况下发出警报的方式,但它似乎不起作用

 if( alarm.DateDay== nowDateDay && alarm.DateMonth== nowDateMonth){
setAlarm(context, calendar,
alarmSet = true;
break;
}

我在这个阶段失去了想法,非常感谢任何帮助。提前致谢

更新

教程允许用户根据时间(例如小时和分钟)设置闹钟,但是我想让用户根据日期(例如日月年)设置闹钟

例如,如果用户将闹钟日期设置为 2016 年 2 月 11 日,则假设闹钟会在该日期到期时响起。所以在这种情况下,明天会响起警报,因为明天是 2016 年 2 月 11 日。现在我想要一种方法来使这种情况如上文所述起作用。

感谢帮助

最佳答案

我只是根据您的需要编辑了您的代码。报警日期为2016年2月11日。请务必替换为您的数据库数据。我认为您已经完成了 AlarmServiceAlarmScreen 类。如果没有,你可以让我知道我会编辑。现在 AlarmManagerHelper 应该如下所示。

public class AlarmManagerHelper extends BroadcastReceiver {

public static final String ID = "id";
public static final String NAME = "name";
public static final String TIME_HOUR = "timeHour";
public static final String TIME_MINUTE = "timeMinute";
public static final String TONE = "alarmTone";

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

public static void setAlarms(Context context) {
cancelAlarms(context);

AlarmDBHelper dbHelper = new AlarmDBHelper(context);

List<AlarmModel> alarms = dbHelper.getAlarms();

for (AlarmModel alarm : alarms) {
if (alarm.isEnabled) {

PendingIntent pIntent = createPendingIntent(context, alarm);

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH,11); //sample date
calendar.set(Calendar.YEAR,2016);//sample year. plz add yours from db
calendar.set(Calendar.MONTH,01);//sample month
calendar.set(Calendar.HOUR_OF_DAY, alarm.timeHour);
calendar.set(Calendar.MINUTE, alarm.timeMinute);
calendar.set(Calendar.SECOND, 00);


//Find next time to set
final int nowDay = Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
final int nowHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
final int nowMinute = Calendar.getInstance().get(Calendar.MINUTE);
final int nowDate = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
final int nowMonth = Calendar.getInstance().get(Calendar.MONTH);
final int nowYear = Calendar.getInstance().get(Calendar.YEAR);
boolean alarmSet = false;
if(nowYear<=2016 && nowMonth<=01 && nowDate<=11 ) //change 2016,01,11 with user given data (from database)
{
if(nowHour<=alarm.timeHour)
{
if (nowMinute<alarm.timeMinute)
{
setAlarm(context, calendar, pIntent);
alarmSet = true;


}
}
}
}
}
}

@SuppressLint("NewApi")
private static void setAlarm(Context context, Calendar calendar, PendingIntent pIntent) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pIntent);
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pIntent);
}
}

public static void cancelAlarms(Context context) {
AlarmDBHelper dbHelper = new AlarmDBHelper(context);

List<AlarmModel> alarms = dbHelper.getAlarms();

if (alarms != null) {
for (AlarmModel alarm : alarms) {
if (alarm.isEnabled) {
PendingIntent pIntent = createPendingIntent(context, alarm);

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pIntent);
}
}
}
}

private static PendingIntent createPendingIntent(Context context, AlarmModel model) {
Intent intent = new Intent(context, AlarmService.class);
intent.putExtra(ID, model.id);
intent.putExtra(NAME, model.name);
intent.putExtra(TIME_HOUR, model.timeHour);
intent.putExtra(TIME_MINUTE, model.timeMinute);
intent.putExtra(TONE, model.alarmTone.toString());

return PendingIntent.getService(context, (int) model.id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
}

关于android - 在 android 中使用 Pending Intent 发出警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35304758/

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