【问题标题】:AlarmManager with notification not firing通知未触发的 AlarmManager
【发布时间】:2015-03-12 23:36:37
【问题描述】:

我正在制作一个具有持久通知的应用,因此我尝试使用AlarmManager 定期更新通知。

我已扩展BroadcastReceiver

public class Notification extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        updateNotification();
    }

如果我只是运行 notification.onReceive,通知会按预期更新,所以我相信问题不在这里。我还在 'MainActivity.onCreatethat is supposed to create and run analarmManager` 中添加了代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent alarmIntent = new Intent(getApplicationContext(), Notification.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);

    AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    manager.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 5000,
        pendingIntent);
}

当我运行应用程序时似乎没有发生任何事情,但我看到 AlarmManager 服务耗尽了我的电池电量,所以我很确定某些东西正在运行。我认为问题出在pendingIntent;要么是那个,要么是我遗漏了一些东西(在项目中没有做任何与AlarmManager 相关的事情)。

在 Android Studio 上编写代码并在我的 Note 4 上运行它。minSdkVersion 19targetSdkVersion 21

非常感谢任何帮助。谢谢!

【问题讨论】:

  • 尝试在onReceive 中记录一些内容。如果你看到了,那么问题不在于AlarmManager
  • 我刚试了一下,消息没有显示出来。

标签: android android-studio alarmmanager android-notifications


【解决方案1】:

好的,原来我正在查看的示例没有提到我必须添加

<receiver android:name=".Notification" />

到清单。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-23
    • 1970-01-01
    相关资源
    最近更新 更多