2013-05-04 33 views
0

我已經設置了在我的應用程序中的多個任務重複報警。我正確地獲取了所有任務通知。但是當我點擊解僱按鈕時,鬧鐘不會取消。 我已經爲用戶創建了警報對話通知。如何停止從其他活動重複鬧鐘

我正在將DB _id傳遞給PendingIntent,作爲UniqueId,用於處理不同任務的多個警報。鬧鈴響起,但沒有停止。請指導我這樣做。

這裏是我的Activity類我在哪裏設置警報

protected void onCreate(Bundle savedInstanceState) { 
// TODO Auto-generated method stub 
super.onCreate(savedInstanceState); 
setContentView(R.layout.alarm_layout); 
// some code.. 
setalarm.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View arg0) { 
     // TODO Auto-generated method stub 

     idval=idval+1; // Fetching this Id from DB. 
     alarm_intent= new Intent(AlarmActivity.this, ReceiverActivity.class); 
     alarm_intent.putExtra("STR_TEXT", str_text); 
     alarm_intent.putExtra("CLS_ID", idval); 
     pendingIntent = PendingIntent.getBroadcast(AlarmActivity.this, idval, alarm_intent, PendingIntent.FLAG_UPDATE_CURRENT); 

     Date dat = new Date();//initializes to now 
     Calendar cal_alarm = Calendar.getInstance(); 
     Calendar cal_now = Calendar.getInstance(); 
     cal_now.setTime(dat); 

    // mYear, mMonth, mDay, mhours24, mminutes taking this vales from DatePicker & TimePicker 

cal_alarm.set(mYear, mMonth, mDay, mhours24, mminutes, 0); 
     if(cal_alarm.before(cal_now)) 
     { 
      cal_alarm.add(Calendar.DATE,1); //if its in the past increment 
     } 

     String temp_val=(String)spinner1.getSelectedItem(); 
     AlarmManager noteam = (AlarmManager)getSystemService(Context.ALARM_SERVICE);     
     if(temp_val.equals("One Time")) 
     { 
      noteam.set(AlarmManager.RTC_WAKEUP, cal_alarm.getTimeInMillis(), pendingIntent); 
     } 
     else if(temp_val.equals("Every 5 Minutes")) 
     { 
      noteam.setRepeating(AlarmManager.RTC_WAKEUP, cal_alarm.getTimeInMillis(), 300000, pendingIntent); 
     } 
     else if(temp_val.equals("Every 10 Mintues")) 
     { 
      noteam.setRepeating(AlarmManager.RTC_WAKEUP, cal_alarm.getTimeInMillis(), 600000, pendingIntent); 
     } 
     else if(temp_val.equals("Every 30 Minutes")) 
     { 
      noteam.setRepeating(AlarmManager.RTC_WAKEUP, cal_alarm.getTimeInMillis(), 1800000, pendingIntent); 
     } 
     else if(temp_val.equals("Every 1 hour")) 
     { 
      noteam.setRepeating(AlarmManager.RTC_WAKEUP, cal_alarm.getTimeInMillis(), 3600000, pendingIntent); 
     } 
    } 
}); 

}

和另一個Activity我在哪裏解除警報

protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 

     //some code.. 
     clsID=extra.getInt("CLS_ID"); 
     new AlertDialog.Builder(AlertBoxNotification.this) 
     .setTitle("Title") 
     .setMessage("Message for user") 
     .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { 
     @SuppressLint("NewApi") 
     @Override 
      public void onClick(DialogInterface arg0, int arg1) { 
      // TODO Auto-generated method stub 
       finish(); 
       arg0.dismiss(); 
      } 
     }) 
     .setNegativeButton(R.string.dismissbtn, new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface arg0, int arg1) { 
      Intent intent = new Intent(AlertBoxNotification.this, AlarmActivity.class); 
        PendingIntent psender = PendingIntent.getBroadcast(AlarmActivity.mycontext = getApplicationContext(), clsID, intent, 0); 
        AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); 
        am.cancel(psender); 
        Toast.makeText(getApplicationContext(), "Alarm cancel ", Toast.LENGTH_LONG).show(); 

      } 
     }).create().show(); 
    } 

回答

1

我必須改變這種

PendingIntent psender = PendingIntent.getBroadcast(AlarmActivity.mycontext = getApplicationContext(), clsID, intent, 0); 

pISender = PendingIntent.getBroadcast(getBaseContext(), clsID, intent, 0); 

並提出PendingIntent全球。 現在警報正在取消另一個活動(從對話框中)。

0

對懸而未解的意圖請求代碼需要兩個地方都一樣。

+0

請求代碼值對於這兩個地方都是相同的。變量名稱是不同的@ gulati – PSK 2013-05-04 13:03:41