gpt4 book ai didi

android - 警报仅在我处于设置它的 Activity 中时触发

转载 作者:行者123 更新时间:2023-11-30 04:46:28 25 4
gpt4 key购买 nike

我正在我的应用程序中从 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();
}

}

编辑:我的 list 中还有以下内容:

<receiver android:process=":remote" android:name="ReminderReceiver"></receiver>

如果我留在Activity在我设置警报的地方收到了很好的警报。如果我点击后退按钮返回我的 ListActivity在列出所有项目或完全离开应用程序的情况下,警报永远不会触发。我在设置警报时是否做错了什么,它只从设置它的 Activity 中触发?

谢谢。

最佳答案

你需要查看 Service而不是用于不与用户交互(如闹钟)的长期进程的 Activity

关于android - 警报仅在我处于设置它的 Activity 中时触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4855662/

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