0

这是我在其中放置警报的保存按钮。但问题是,如果我添加 1 个警报,它会很好用,但是如果我在最后一个警报触发之前的下一分钟添加一个警报,它会取消最后一个警报并更新到新警报,我尝试更改请求代码,但它对我不起作用

        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);

}
4

2 回答 2

1

尝试摆脱FLAG_UPDATE_CURRENT你的PendingIntent.

于 2012-04-20T15:42:45.383 回答
0

生成的请求代码不起作用,因为我添加了这一行并使用 intent_id 作为我的请求代码,它运行得非常好

final int intent_id= (int) System.currentTimeMillis();

于 2012-04-20T18:55:22.390 回答