仅当我处于设置警报的活动中时警报才会触发
我正在通过 ItemEdit Activity
在我的应用中创建警报。人们可以在其中编辑/查看其笔记/待办事项,还可以为该项目设置提醒/闹钟。我使用以下代码设置闹钟:
private void createAlarm() {
Intent intent = new Intent(this, ReminderReceiver.class);
intent.putExtra("reminder_message", "Reminder Received!");
intent.putExtra("item_id", mRowId);
PendingIntent sender =
PendingIntent.getBroadcast(
getApplicationContext(),
ALARM_ID,
intent,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
// Get the AlarmManager service
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
// Set alarm to the time given by the user.
am.set(AlarmManager.RTC_WAKEUP, mReminderCal.getTimeInMillis(), sender);
}
这是接收器
public class ReminderReceiver extends BroadcastReceiver {
private static final String TAG = "MyApp";
@Override
public void onReceive(Context context, Intent intent) {
try {
Bundle bundle = intent.getExtras();
String message = bundle.getString("reminder_message");
Log.v(TAG, message);
} catch(Exception e) {
Log.v(TAG, "OH SNAP!");
e.printStackTrace();
}
}
编辑:我的清单中还有以下内容:
如果我停留在设置闹钟的 Activity
中,则接收效果良好。如果我点击后退按钮返回到列出所有项目的 ListActivity
或完全离开应用程序,则警报永远不会触发。我在设置闹钟时是否做错了什么,它只能从设置它的活动中触发?
谢谢。
I'm creating an alarm in my app from the ItemEdit Activity
. Its where one can edit/view their note/todo item, they can also set a reminder/alarm for the item there. I set the alarm with the following code:
private void createAlarm() {
Intent intent = new Intent(this, ReminderReceiver.class);
intent.putExtra("reminder_message", "Reminder Received!");
intent.putExtra("item_id", mRowId);
PendingIntent sender =
PendingIntent.getBroadcast(
getApplicationContext(),
ALARM_ID,
intent,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
// Get the AlarmManager service
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
// Set alarm to the time given by the user.
am.set(AlarmManager.RTC_WAKEUP, mReminderCal.getTimeInMillis(), sender);
}
And here is the Receiver
public class ReminderReceiver extends BroadcastReceiver {
private static final String TAG = "MyApp";
@Override
public void onReceive(Context context, Intent intent) {
try {
Bundle bundle = intent.getExtras();
String message = bundle.getString("reminder_message");
Log.v(TAG, message);
} catch(Exception e) {
Log.v(TAG, "OH SNAP!");
e.printStackTrace();
}
}
Edit: Also I have the following in my manifest:
<receiver android:process=":remote" android:name="ReminderReceiver"></receiver>
If I stay in the Activity
where I set the alarm it is received fine. If I hit the back button to return to my ListActivity
where all the items are listed or leave the app entirely the alarm never triggers. Have I done something wrong in setting up my alarm that it only triggers from the Activity that set it?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要查看 Service 而不是
Activity< /code> 用于不与用户交互的长期进程(如闹钟)。
You need to look into a Service instead of an
Activity
for a long-living process that doesn't interact with the user (like an alarm clock).