2013-05-04 4 views
0

Я установил повторение тревоги по нескольким задачам в своем приложении. Я получаю все уведомления о задании должным образом. но когда я нажимаю кнопку увольнения, будильник не отменяется. Я создал уведомление Alert Dialog пользователю.как остановить повторение сигнала тревоги от другой активности

Я прохожу DB _id до PendingIntent как UniqueId для нескольких аварийных сигналов по различным задачам. будильника правильно, но не останавливается. пожалуйста, помогите мне сделать это.

Вот мой класс активности, где я установка сигнализации

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

Смежные вопросы