我正在開發一個提醒應用程序,其中我使用sqlite將所有提醒 相關數據存儲在數據庫中,一切工作正常。使用AlarmManager取消鬧鐘
問題:
當我從數據庫中刪除,我還試圖把 報警斷行,但它不工作,我使用recyclerview。
請幫我找出我的代碼中有什麼問題。
這裏是我的代碼:
DataAdapter.java
dataAdapterViewHolder.deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int deleteRowId = context.getContentResolver().delete(RoutineContract.RoutinesEntry.CONTENT_URI,
rowSelection, rowSelectionArgs);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
PendingIntent alertIntent = PendingIntent.getBroadcast(context, rowId, intent, PendingIntent.FLAG_NO_CREATE);
if(alertIntent != null) {
alarmManager.cancel(alertIntent);
alertIntent.cancel();
Log.e("row id", ""+rowId);
}
}
});
AddReminderActivity.java
Uri insertUri = getContentResolver().insert(RoutineContract.RoutinesEntry.CONTENT_URI, contentValues);
int rowId = Integer.parseInt(insertUri.getLastPathSegment());
Intent intent = new Intent(this, AlarmReceiver.class);
intent.setData(insertUri);
intent.putExtra("name", name);
intent.putExtra("repeatType", repeatType);
PendingIntent alertIntent = PendingIntent.getBroadcast(this, rowId, intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, dateTime.getTimeInMillis(),
pendingIntent);
PS:我已經搜查計算器和發現解決問題的辦法但現在沒有人爲我工作。
我也嘗試過使用FLAG_UPDATE_CURRENT和FLAG_ONE_SHOT,但那對我來說不起作用 –
請參閱我編輯的答案,並嘗試添加setData(yourUri)時也取消報警 –
感謝工作,它在文檔中提到,取消pendingIntent應該與創建時使用的匹配,我忘了這一點。 –