【问题标题】:Set an alarm at specific date and time在特定日期和时间设置闹钟
【发布时间】:2013-08-19 20:33:07
【问题描述】:

我正在尝试在我的应用程序中设置警报。每当警报启动时,都会向用户显示AlertDialog

我创建了两个活动:

  • ActivityA是负责设置闹钟的人
  • ActivityB负责向用户显示AlertDialog窗口

ActivityA:设置闹钟

报警日期存储在GregorianCalendar类型的变量alarmDate中。我使用以下代码创建了警报(按照答案here):

Intent intent = new Intent(this, ActivityB.class);
intent.putExtra(ActivityB.ALARM_MESSAGE, message);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, alarmDate.getTimeInMillis(), pendingIntent);

ActivityB:处理请求

public void onCreate(Bundle savedInstanceState) {
    Intent intent = getIntent();
    String message = intent.getStringExtra(ALARM_MESSAGE);      
    AlertDialog dialog = new AlertDialog.Builder(this).setMessage(message)
                       .setPositiveButton("OK", new DialogInterface.OnClickListener()...)
                       .create();
    dialog.show();
    finish();
}

问题

我遇到了一个问题:ActivityB 无法捕获意图。因此,我认为它永远不会被创建,我错过了一些关于如何启动它的东西。

此外,我认为这不是最理想的解决方案,因为我需要警报来显示AlertDialog 窗口,以防应用程序未运行。

关于如何解决这些问题的任何提示?

谢谢。

【问题讨论】:

    标签: android alarmmanager


    【解决方案1】:

    您正在使用PendingIntent.getService,但应该使用getActivity

    UPD 关于 AlertDialog,您可以使用透明活动并仍然打开 AlertDialog。

    【讨论】:

    • 我试过用第二种方法切换,但不幸的是,它似乎又静音了。可能在 onCreate 方法中处理的东西是错误的?
    • 你为什么叫finishonCreate?
    • 活动必须执行的所有代码都在 onCreate 方法中。除此之外,我尝试调试代码:执行到达该方法从未发生过。
    • 好吧,我们只是在这里做假设。这很难说。您是否在 android manifest 中定义了活动?你怎么知道它永远不会到达活动的 onCreate?
    猜你喜欢
    • 2011-12-29
    • 1970-01-01
    • 1970-01-01
    • 2016-09-25
    • 1970-01-01
    • 2011-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多