我試圖做出Android提醒,但出現了問題;當我在我的應用程序中設置重複通知時,通知會一直開着,直到我卸載應用程序(刪除重複通知並不會阻止它關閉)。考慮到這一點,當我刪除它們時,我的提醒將從數據庫中刪除。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);
}
}
編輯
問題在我的情況是不是取消報警,我不知道爲什麼,當我即使在刪除提醒我他們只是繼續下去。
可能重複[如何取消此重複警報?](http://stackoverflow.com/questions/3330522/how-to-cancel-this-repeating-alarm) –
然後,你在哪裏取消警報?它們不會因爲您刪除應用程序數據庫中的條目而停止。 –
@Mike M,非常感謝。你說得對,我也必須取消我的警報。 – pablo