- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
在 Android 警报管理器中,我们如何安排多个不重复且没有固定间隔重复的警报?我不能使用“setRepeating”功能,因为警报没有任何重复模式。
我将警报时间存储在 Sqlite 数据库表中, Activity 应从该表中选择日期和时间并设置警报。
如果我们在一个循环中设置不同的警报,那么它只保留最后一个。我从帖子中读到:How can create more than one alarm?
它告诉将唯一 ID 附加到 Intent ,然后设置单独的警报事件。但这对我不起作用。
我们需要在 Manifest 文件中添加什么来处理这个唯一的 id 吗?
Activity “RegularSchedule”中的代码是,它只创建一个警报事件:
while (notifCursor.moveToNext()) {
Intent intent = new Intent(RegularSchedule.this,
RepeatingAlarm.class);
// The cursor returns first column as unique ID
intent.setData(Uri.parse("timer:" + notifCursor.getInt(0)));
PendingIntent sender = PendingIntent.getBroadcast(
RegularSchedule.this, 0, intent, 0);
// Setting time in milliseconds taken from database table
cal.setTimeInMillis(notifCursor.getLong(1));
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
}
如果需要更多详细信息或代码 fragment ,请告诉我。
list 文件(此处为 RepeatingAlarm 扩展 BroadcastReceiver):
<receiver android:name=".user_alerts.RepeatingAlarm" android:process=":remote" />
<activity android:name=".user_alerts.RegularSchedule"
android:label="@string/reg_schedule_title" android:theme="@android:style/Theme.Light">
</activity>
重复警报:
public class RepeatingAlarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
.......
// The PendingIntent to launch our activity if the user selects this notification
Intent notificationIntent = new Intent (context, DisplayReminder.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
// Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
mNotificationManager.notify(2425, notification);
最佳答案
这对我有用。我正在分享解决方案,以便其他人受益并找到解决此问题的快速解决方案。
我欢迎任何其他意见,以便更多地了解解决方案的技术性以及为什么某些事情有效而其他事情无效:)
(1) 首先,Manifest 文件:确保您的类(class)拥有 BroadcastReceiver 的接收器。
<receiver android:name=".RepeatingAlarm" android:process=":remote">
<intent-filter>
<data android:scheme="timer:" />
</intent-filter>
</receiver>
请注意,该类是主包的一部分。如果它在某些子包中,请移至主包。主包是你在'manifest'标签中定义的。
'intent-filter'用于定义'action'和'data'。您可以将 Activity 类放在这里,该类将从您的待处理 Intent 中调用。但我发现如果你在 list 中定义“ Action ”,它不会在 Activity 上显示动态值。它只显示静态值。很奇怪。如果您遇到同样的问题,请不要将“ Action ”放在 list 中,而是将其作为待处理 Intent 的一部分放在 BroadcastReceiver 类中。
'data' 标签是您在使用 AlarmManager 调度不同警报时要放置的唯一 Intent 的动态 URI。请参阅后续步骤了解更多详情。
(2) Activity 类,您将在其中使用 AlarmManager 来安排警报:我正在使用数据库来存储我的闹钟时间值,然后使用这些值进行调度。我的光标从表中获取唯一的 _ID 和警报时间(自 1970 年 1 月 1 日以来的秒数)。看到放在这里的 URI 和你在 list 文件中的一样。
Calendar cal = Calendar.getInstance();
int notifIterator = 0;
if (notifCursor.getCount() > 0) {
while (notifCursor.moveToNext()) {
Intent intent = new Intent(MySchedule.this,
RepeatingAlarm.class);
// As the same intent cancels the previously set alarm having
// same intent
// changing the intent for every alarm event so that every alarm
// gets
// scheduled properly.
intent.setData(Uri.parse("timer:" + notifCursor.getInt(0)));
PendingIntent sender = PendingIntent.getBroadcast(
MySchedule.this, 0, intent,
Intent.FLAG_GRANT_READ_URI_PERMISSION);
cal.setTimeInMillis(notifCursor.getLong(1) * 1000);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
notifIterator++;
Toast mToast = Toast.makeText(
RegularSchedule.this,
"Reminders added to the calendar successfully for "
+ android.text.format.DateFormat.format(
"MM/dd/yy h:mmaa",
cal.getTimeInMillis()),
Toast.LENGTH_LONG);
mToast.show();
}
}
如果您在执行此操作后仍未看到警报,请检查模拟器所采用的时区。有时,我们安排本地时区,但模拟器安排 GMT 时区。如果您查看 toast 消息,这将帮助您解决此问题。
(3) 最后一个是 BroadcastReceiver 类。请注意,要打开数据库,您需要使用“上下文”:
public void onReceive(Context context, Intent intent) {
// Update the status in the notification database table
int notificationId = Integer.parseInt(intent.getData().getSchemeSpecificPart());
db = context.openOrCreateDatabase(DATABASE_NAME,
SQLiteDatabase.CREATE_IF_NECESSARY, null);
<<<< Do DB stuff like fetching or updating something>>>>
// Raise the notification so that user can check the details
NotificationManager mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
int icon = R.drawable.icon;
CharSequence tickerText = "your text";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
// Count of number of notifications
notification.number = notifCount;
CharSequence contentTitle = "your title ";
CharSequence contentText = "your notification text";
// The PendingIntent to launch our activity if the user selects this
// notification
Intent notificationIntent = new Intent(context, DisplayReminder.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
// Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(context, contentTitle, contentText,
contentIntent);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
// Instead of 1234 or any other number, use below expression to have unique notifications
// Integer.parseInt(intent.getData().getSchemeSpecificPart())
mNotificationManager.notify(1234, notification);
}
请注意,如果您想创建单独的通知,可以在调用 notify() 时将请求 id 作为唯一传递。
最后,您可以创建当用户点击通知时要调用的 DisplayReminder 类。
关于android - 警报管理器 - 安排多个非重复事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6649402/
我正在尝试将字符串转换为 float 。我知道 parseFloat() 可以做到这一点,但我也找到了下面的语法,但没有太多引用。 什么是正确的语法,因为它们似乎都有效。我可以在哪里了解更多相关信息?
我见过一些看起来很酷的“窗口”/“警报”/不管它们叫什么。我希望我知道。以下是其中的一些示例: 这些不应该是 Apple 独有的,因为我已经看到 3rd 方应用程序使用它们!我想知道这些 window
这个问题已经有答案了: What is the difference between a function call and function reference? (6 个回答) 已关闭 7 年前。
alert('test1'); var re = new RegExp("(http://(?:[A-Za-z0-9-]+\\.)?[A-Za-z0-9-]+\\.[A-Za-z0-9-]+/?)",
我有一个 Rails 应用程序,它与其他 Rails 应用程序通信以进行数据插入。我使用 jQuery $.post 方法进行数据插入。对于插入,我的其他 Rails 应用程序显示 200 OK。但在
我的作业有问题...我不知道我的代码有什么问题..我的作业是创建一个简单的学习数学和级别选择......我使用下拉菜单来选择级别和算术运算......现在我的问题是,当我单击按钮时,它将转到函数sta
我有一些复选框,其值属性来自数据库。我希望用户始终检查具有相同值的复选框(如果他不使用 javascript 发出警报)。我尝试使用以下 javascript 代码执行此操作,但没有用 fu
这有点难以解释,我的网站上有一个幻灯片形式的多部分表单。他们必须使用单选按钮从 3/4 选项中进行选择。 我对它们进行了一些验证,以确保用户在允许转到下一张幻灯片之前选择一个。 如果我单击一个对象来选
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 5 年前。 Improve this ques
我的页面上有一个click链接,我想在单击时播放通知或提示音。我如何使用jQuery做到这一点? 在此先感谢... :) 炸药 最佳答案 使用 jQuery sound 插件。 关于javascrip
我正在尝试在 Excel 列中创建 VBA -Alert 弹出窗口。在基于某些计算的 Excel 工作表中,将计算一些增长%(H 列),如果增长%> 20%,则会生成一个警报弹出窗口,询问原因代码,需
当用户滚动到网页的特定部分时,如何使用 JavaScript 显示警报。我尝试通过检查 document.body.clientWidth = document.documentElement.cli
我正在尝试制作一个脚本,其中会弹出一个提示窗口询问问题,并根据其中的答案,会弹出一个警告框,指出答案有效或无效。在我的代码中,我的提示框有效,但我的警报框没有。有人可以帮我解决这个问题吗?非常感谢!!
我正在尝试 Grafana 的警报和通知功能,它看起来真的很棒。 松弛通知示例。 但是有一个大问题。它需要使用 S3 进行配置,这使得任何人都可以公开访问图像。对于那些不希望公开访问其图像的公司来说,
我想知道是否有任何方法可以在 adobe reader 中通知用户pdf 表单已提交到服务器?我正在提交一个正常的 http/html 形式到 php 脚本没什么大不了的,直接,但文档、论坛等似乎存在
在 TFS 中构建失败后,是否可以通过电子邮件获取构建成功的信息? 当构建失败时(我确实如此),我可以收到电子邮件。当构建成功时,我可以收到电子邮件。 但我需要知道构建不再失败。如果我收到构建失败的电
我需要一些帮助来理解 jQuery 如何存储元素。请看一下这个链接: http://jsfiddle.net/NubWC/ 我试图从所有具有特定类的标题标签中获取元素 id,并将其放入数组中,以便我可
我想做 alert(this) 来进行演示(想看看代码中不同位置的“this”是什么)。 有什么想法可以实现这一目标吗? 现在它只返回[object Object]? 最佳答案 这样做: consol
当出现警告框时,有什么方法可以阻止 Enter 键盘吗?因此用户需要按 Esc 键或单击 Ok 按钮来删除警报。 alert('Hello'); 最佳答案 正如我之前的评论所述,标准的 javas
我正在尝试在 javascript 中创建一个函数并传入参数“name”,然后当用户点击一张照片时,会出现一条警告,类似于“这张照片是在 ____ 拍摄的” function photoWhere
我是一名优秀的程序员,十分优秀!