[英]AlarmManager with notification not firing
I'm making an app with a persistent notification, so I'm trying to use AlarmManager
to periodically update the notification. 我正在制作具有持久通知的应用程序,因此我尝试使用
AlarmManager
定期更新通知。
I've extended BroadcastReceiver
: 我扩展了
BroadcastReceiver
:
public class Notification extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
updateNotification();
}
If I just run notification.onReceive, the notification updates as expected, so I believe the problem isn't here. 如果我只运行notification.onReceive,则通知会按预期更新,因此我认为问题不在这里。 I've also added code in 'MainActivity.onCreate
that is supposed to create and run an
alarmManager`: 我还在“ MainActivity.onCreate”
that is supposed to create and run an
添加了that is supposed to create and run an
alarmManager的代码:
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);
}
Nothing appears to happen when I run the app, but I see the AlarmManager
service draining my battery, so I'm pretty sure something is running. 运行该应用程序时似乎什么也没发生,但是我看到
AlarmManager
服务耗尽了电池电量,因此我可以肯定正在运行。 I think the problem is with pendingIntent
; 我认为问题在于
pendingIntent
; Either that or I am missing something (haven't done anything else in the project relating to AlarmManager
). 或者是我丢失了一些东西(项目中没有做任何与
AlarmManager
有关的AlarmManager
)。
Coding this on Android Studio and running it on my Note 4. minSdkVersion 19
, targetSdkVersion 21
将其编码在Android Studio上并在我的Note 4上运行
targetSdkVersion 21
minSdkVersion 19
, targetSdkVersion 21
Any help is greatly appreciated. 任何帮助是极大的赞赏。 Thanks!
谢谢!
Ok, turns out the example I was looking at didn't mention that I had to add 好的,原来的例子我没有提到我必须添加
<receiver android:name=".Notification" />
to the manifest. 到清单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.