我是Android新手,我正在創建日期提醒應用程序,用戶將在輸入日期&時輸入日期,時間和消息。到目前爲止,我已經實現了創建警報,顯示通知,單擊通知,打開具有該通知的詳細信息並根據需要進行更新,列出所有警報(我使用SqlLite存儲數據)。無法從AlarmManager中取消PendingIntent
在我的報警列表頁面上,點擊項目時,我顯示AlertDialog框中有刪除選項,當我點擊刪除時,我正在從數據庫中刪除報警細節,但無法取消報警。
以下是來自2個類的代碼,其中一個是創建鬧鐘的地方,另一個是我取消鬧鐘的地方,不知道我錯過了什麼!
Class CreateActivity extends Activity
methode setAlarm{
.....
Intent myIntent = new Intent(this.getApplicationContext(), AlarmReciever.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
db = new DatabaseHandler(this);
if(!isNotified) //create new alarm
{
Random random = new Random();
alarmId = random.nextInt(10000);
pojo = new DateBookPojo(alarmId, inDate, inTime, inMsg);
db.addReminder(pojo);
System.out.println("Adding Reminder.");
}else{ // Its from notification
pojo = new DateBookPojo(alarmId, inDate, inTime, inMsg);
db.updateReminder(pojo);
System.out.println("Updating Reminder.");
}
myIntent.putExtra("alarmId", alarmId);
pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), alarmId, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
....
}
Here AlarmId is unique passed to PendingIntent
這裏是另一個列表活動,其中點擊AlertDialog我從數據庫中刪除報警,並試圖取消報警管理器。在這裏,我用同樣的AlarmId這是用來創建的PendingIntent
Class MyListActivity extends ListActivity
....
private void deleteReminder(int alarmId) {
System.out.println("alarmId= "+ alarmId);
DatabaseHandler db = new DatabaseHandler(this);
DateBookPojo pojo = new DateBookPojo();
pojo.setReminderId(alarmId);
db.deleteReminder(pojo); // delete alarm details from database
System.out.println("Deleted Entry. = > " + alarmId);
Intent myIntent = new Intent(this.getApplicationContext(), AlarmReciever.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), alarmId, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
System.out.println("Cancelled Alarm. = > " + alarmId);
showList(); // Again show the new list.
}
我GOOGLE &計算器了很多,但我到處看到這個解決方案工作正常,但它不是爲我工作。
有人想到我在這裏指出,通知Onlick,我使用CreateActivity
類來顯示細節和更新時,它更新通知的警報。我想我正在搞一些與Intent
& PendingIntent
相關的背景。
請幫幫我。
解答@ 323go建議爲我工作的聲明。感謝您的回覆。 – Swaraj