2012-04-20 58 views
0

这是我的保存按钮,我在其中放置警报。但问题是,如果我添加1个报警它的伟大工程,但如果我增加一个报警下一分钟前的最后一次报警触发时,它会取消最后一个报警和更新,以新的,我试图改变请求的代码,但它不是为我工作Android中使用Android服务的多个警报

 save = (Button)findViewById(R.id.save_rem); 
     save.setOnClickListener(new OnClickListener() 
     { 

      public void onClick(View arg0) 
      { 
       title_txt = title.getText().toString(); 
       description_txt = description.getText().toString(); 
       id++; 


       addRemInDb a = new addRemInDb(); 
       int v = a.insert(title_txt, hour, minute, mMonth, mDay, mYear, description_txt); 
       //a.deleteTable(); 
       a.closeDB(); 
       if(v == -1) 
       { 
        Toast.makeText(getApplication(), "Title & Time Must Not Be Same",Toast.LENGTH_LONG).show(); 
       } 
       else 
       {            
        Calendar cal = Calendar.getInstance(); 

        //Setting alarm to be triggered on specified date and time 
        cal.set(mYear, mMonth, mDay, hour, minute, seconds); 

        requestcode++; // Here request code is adding every time when i click in save button 

        Intent alarmIntent = new Intent(getApplicationContext(), AlarmReceiver.class); 

        alarmIntent.putExtra("title", title_txt);   //This is title for notification 
        alarmIntent.putExtra("note", description_txt);  //This is Description for notification 
        alarmIntent.putExtra("rq", requestcode); 

        PendingIntent sender = PendingIntent.getService(getApplicationContext(), requestcode, alarmIntent,PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA); 



        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 
        alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);      

        finish(); 


       }  

      } 
     }); 

这是我AlarmReciever类

public class AlarmReceiver extends Service 
{ 
@Override 
public void onCreate() 
{ 

} 

@Override 
public void onStart(Intent intent, int startId) 
{ 
    super.onStart(intent, startId);  


    Bundle extras = intent.getExtras(); 

    String title=extras.getString("title"); 
    String note=extras.getString("note"); 
    int requestcode = extras.getInt("rq"); 


    NotificationManager nManger = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 

    Notification notification = new Notification(R.drawable.notify, "Reminder", System.currentTimeMillis()); 

    PendingIntent contentIntent = PendingIntent.getService(getApplicationContext(), requestcode, new Intent(getApplicationContext(), AlarmReceiver.class), 0); 

    notification.setLatestEventInfo(getApplicationContext(), note, title, contentIntent); 
    notification.flags = Notification.FLAG_INSISTENT; 
    notification.defaults = Notification.DEFAULT_SOUND; 

    nManger.notify(NOTIFICATION_ID, notification); 

} 

回答

0

的Requestcode IM产生不能正常工作,因为我加入这一行 并用于intent_id作为我的请求的代码,它的工作完全正常

最终诠释intent_id =(int)的System.currentTimeMillis的();

+0

如何删除所有报警? – 2012-07-16 16:02:43

+0

如果您通过待定意图和警报管理器创建警报,则可以将警报1删除1.为此,您必须再次创建类似于您在设置警报时创建的待定意图。然后用alarmmanager.cancel()函数可以删除报警。请记住,您在待处理意图中的请求代码应该与设置警报时所用的相同。 – 2012-07-16 22:13:36

1

尝试让你的PendingIntent摆脱FLAG_UPDATE_CURRENT