【问题标题】:Android: How to start a service from a scheduled alarm?Android:如何从预定的警报启动服务?
【发布时间】:2014-01-25 21:56:16
【问题描述】:

我就是不能让我的闹钟被解雇。所有有关取消、保存和重启后恢复的问题都已得到处理。只是在预期的时间什么都没有发生。我试图启动一个活动而不是服务,但没有区别。实际上,我有点想知道,因为它没有说明 Service 对象不仅应该被创建,而且应该被启动。这是因为类型而自动的吗?从 Activity 直接调用 startService() 可以正常工作,因此问题出在警报中,而不是在服务中。很遗憾,当前应用设置的闹钟无法列出来检查是否一切正常。

感谢任何帮助。

long INTERVAL_WEEK = AlarmManager.INTERVAL_DAY * 7;
DateFormat df = new SimpleDateFormat("E, yyyy-MM-dd'T'HH:mmZ");
Log.d(TAG, "next alarm " + df.format(cal.getTime())); // checking the right time
Intent showNotificationIntent = new Intent(context, MyNotificationService.class);
alarmIntent = PendingIntent.getBroadcast(context, dayOfWeek, showNotificationIntent, 0);
getAlarmManager(context).setInexactRepeating(AlarmManager.RTC, cal.getTimeInMillis(),
    INTERVAL_WEEK, alarmIntent);

【问题讨论】:

    标签: android service alarmmanager android-pendingintent


    【解决方案1】:

    在创建 PendingIntent 时,您应该使用 PendingIntent.getService() 而不是 PendingIntent.getBroadcast()。

    【讨论】:

    • 太棒了。我不明白为什么会有各种 get...() 方法。谢谢!
    猜你喜欢
    • 2010-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-04
    相关资源
    最近更新 更多