我曾尝试制作Android提醒,但出现了问题;当我在我的应用程序中设置重复通知时,通知会继续触发,直到我卸载应用程序(删除重复通知并不会阻止它关闭)。考虑到这一点,我删除它时会从数据库中删除我的提醒。
我的重复警报也是如此。
我该怎么办?
以下是我的代码的一些部分:
//数据库类中的删除查询
public boolean deleteReminder(long rowId) {
return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}
//如何删除主要活动中的提醒
@Override
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.menu_delete:
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
mDbHelper.deleteReminder(info.id);
fillData(); // populate
return true;
}
return super.onContextItemSelected(item);
}
ReminderManager.java
public class ReminderManager {
private Context mContext;
private AlarmManager mAlarmManager;
public ReminderManager(Context context) {
mContext = context;
mAlarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
}
public void setReminder(Long taskId, Calendar when) {
Intent i = new Intent(mContext, OnAlarmReceiver.class);
i.putExtra(RemindersDbAdapter.KEY_ROWID, (long)taskId);
PendingIntent pi = PendingIntent.getBroadcast(mContext, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);
mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), 60 * 1000, pi);
}
}
ReminderService.java
public class ReminderService extends WakeReminderIntentService {
public ReminderService() {
super("ReminderService");
}
@Override
void doReminderWork(Intent intent) {
Log.d("ReminderService", "Doing work.");
Long rowId = intent.getExtras().getLong(RemindersDbAdapter.KEY_ROWID);
NotificationManager mgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(this, ReminderEditActivity.class);
notificationIntent.putExtra(RemindersDbAdapter.KEY_ROWID, rowId);
PendingIntent pi = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT);
Notification note=new Notification(android.R.drawable.stat_sys_warning, getString(R.string.notify_new_task_message), System.currentTimeMillis());
note.setLatestEventInfo(this, getString(R.string.notify_new_task_title), getString(R.string.notify_new_task_message), pi);
note.defaults |= Notification.DEFAULT_SOUND;
note.flags |= Notification.FLAG_AUTO_CANCEL;
int id = (int)((long)rowId);
mgr.notify(id, note);
}
}
OnAlarmReceiver.java
public class OnAlarmReceiver extends BroadcastReceiver {
private static final String TAG = ComponentInfo.class.getCanonicalName();
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Received wake up from alarm manager.");
long rowid = intent.getExtras().getLong(RemindersDbAdapter.KEY_ROWID);
WakeReminderIntentService.acquireStaticLock(context);
Intent i = new Intent(context, ReminderService.class);
i.putExtra(RemindersDbAdapter.KEY_ROWID, rowid);
context.startService(i);
}
}
修改
在我的情况下,问题不是取消警报,我想知道为什么当我甚至删除我的应用程序中的提醒时,他们就会继续关闭。
答案 0 :(得分:0)
当您致电AlarmManager.cancel(PendingIntent)
以取消闹钟时,您必须使用完全相同的PendingIntent
致电setRepeating
。
答案 1 :(得分:0)
您应该取消为重复闹钟而创建的pendingIntent。编写类似buttonclick的代码来执行
AlarmManager.cancel(的PendingIntent)
确保您提供的是与设置中使用的相同的待处理事件。否则,与提供的匹配的其他pendingIntent将被取消。