[英]AlarmManager doesn't fire
my note app should remember my users with a notification at their note at a specific date and time the user had set.我的笔记应用程序应该记住我的用户,并在用户设置的特定日期和时间在他们的笔记中收到通知。 My Problem is that I don't get the notification at the set time.
我的问题是我没有在设定的时间收到通知。
This is my alarmManager.class (Here I create my Alarms)这是我的 alarmManager.class(这里我创建了我的警报)
public class alarmManager {
public static void createAlarm(long timeInMillis, int requestCode, Intent i) {
PendingIntent pi = PendingIntent.getService(MainActivity.context, requestCode, i, PendingIntent.FLAG_UPDATE_CURRENT);
getAlarmManager().set(AlarmManager.RTC_WAKEUP, timeInMillis, pi);
}
public static void deleteAlarm(Intent i, int requestCode) {
if (doesPendingIntentExist(MainActivity.context, i, requestCode)) {
PendingIntent pi = PendingIntent.getService(MainActivity.context, requestCode, i, PendingIntent.FLAG_NO_CREATE);
pi.cancel();
getAlarmManager().cancel(pi);
}
}
private static AlarmManager getAlarmManager() {
return (AlarmManager) MainActivity.context.getSystemService(Context.ALARM_SERVICE);
}
private static boolean doesPendingIntentExist(Context context, Intent i, int requestCode) {
PendingIntent pi = PendingIntent.getService(context, requestCode, i, PendingIntent.FLAG_NO_CREATE);
return pi != null;
}
}
When the user creates a new note this lines get fired (They send everything is needed to my alarmManager.class)当用户创建新笔记时,此行会被触发(他们将需要的所有内容发送到我的 alarmManager.class)
Intent i = new Intent(MainActivity.context, NotificationService.class);
i.putExtra(NotificationService.i_UID, id);
i.putExtra(NotificationService.i_TEXT, title);
i.putExtra(NotificationService.i_CONTENT, content);
i.putExtra(NotificationService.i_REMINDEDATE, mUserReminderDate);
i.putExtra(NotificationService.i_REPEAT, repeatAlarmSelectedOption);
alarmManager.createAlarm(mUserReminderDate.getTime(), id.hashCode(), i);
When everything works the NotificationService.class should get called at the chosen time.当一切正常时,NotificationService.class 应该在选定的时间被调用。 (Here the notification should show to the user)
(此处应向用户显示通知)
public class NotificationService extends IntentService {
public static final String i_UID = "uuid";
public static final String i_TEXT = "title";
public static final String i_CONTENT = "content";
public static final String i_REMINDEDATE = "remindeDate";
public static final String i_REPEAT = "repeatAlarm";
private String mTodoText, mTodoContent, mTodoUUID;
private Date mTodoDate;
private int mTodoRepeat;
private Intent processIntent;
public NotificationService() {
super("NotificationService");
}
@Override
protected void onHandleIntent(Intent intent) {
this.processIntent = intent;
mTodoText = intent.getStringExtra(i_TEXT);
mTodoContent = intent.getStringExtra(i_CONTENT);
mTodoUUID = intent.getStringExtra(i_UID);
mTodoDate = (Date) intent.getSerializableExtra(i_REMINDEDATE);
mTodoRepeat = intent.getIntExtra(i_REPEAT, 0);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification.Builder(this)
.setContentTitle(mTodoText)
.setContentText(mTodoContent)
.setSmallIcon(R.drawable.app_logo_transparent_bg)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_SOUND)
.build();
manager.notify(100, notification);
}
Unfortunately it doesn't work.不幸的是它不起作用。 No errors.
没有错误。 No notification.
没有通知。 Nothing.
没有。
I hope someone can help me.我希望有一个人可以帮助我。
In my opinion the problem is when you create a PendingIntent
:在我看来,问题是当您创建
PendingIntent
:
PendingIntent pi = PendingIntent.getService(MainActivity.context, requestCode, i, PendingIntent.FLAG_UPDATE_CURRENT);
To be absolutely sure, schedule an alarm, then run from adb
following command: adb shell dumpsys alarm
and search for your PendingIntent
.绝对确定,安排警报,然后从
adb
运行以下命令: adb shell dumpsys alarm
并搜索您的PendingIntent
。 If you find it, schedule one more time alarm and run command one more time.如果找到它,请再安排一次警报并再运行一次命令。 Look again for your pending intent and make sure it is a new one and try to search for this previous scheduled.
再次查找您的待定意图并确保它是一个新意图,然后尝试搜索先前预定的意图。 If it is problem with
PendingIntent
you will find only the newest PendingIntent
.如果
PendingIntent
有问题,您只会找到最新的PendingIntent
。
Solution:解决方案:
Since AlarmManger
while comparing PendingIntents
doesn't compare extra data passed it, but compares request code, data, action and categories you need to change one of them every time you schedule a new alarm.由于
AlarmManger
在比较PendingIntents
不会比较传递给它的额外数据,而是比较请求代码、数据、操作和类别,因此您每次安排新警报时都需要更改其中之一。 My suggestion is to pass uri
from database to every new scheduled PendingIntent
.我的建议是将
uri
从数据库传递到每个新的预定PendingIntent
。 That will make each of them uniqal so the older one won't be cancelled when a new scheduled.这将使他们每个人都独一无二,因此在安排新的时不会取消旧的。 To sum up, to argument
i
in your code call method setData()
and pass uri as an argument.总而言之,在您的代码调用方法
setData()
以参数i
并将 uri 作为参数传递。 You can do it in a method or from a class where method is being called.您可以在方法中或从调用方法的类中执行此操作。 It may look like that:
它可能看起来像这样:
public static void createAlarm(long timeInMillis, int requestCode, Intent i, Uri uri) {
i.setData(uri)
PendingIntent pi = PendingIntent.getService(MainActivity.context, requestCode, i, PendingIntent.FLAG_UPDATE_CURRENT);
getAlarmManager().set(AlarmManager.RTC_WAKEUP, timeInMillis, pi);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.