在发出通知警报之前从应用程序中永久取消设置通

时间:2013-08-23 08:50:41

标签: android android-intent

在我的应用程序中,当我删除提醒(同时为多个事件设置提醒)时,提醒闹钟在预定时间后一小时后出现。 有人面临同样的问题吗?有没有人有这方面的解决方案。 在我的应用程序中使用的代码

下面

通知接收者页面

public class NotificationReceiver extends BroadcastReceiver {

    //private static int NOTIFICATION_ID = 1;
    private static int NOTIFICATION_ID;
    @SuppressWarnings("deprecation")
    @Override
    public void onReceive(Context context, Intent intent) 
    {
        Bundle extras=intent.getExtras();
        NotificationManager manger = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(R.drawable.ic_launcher, "Combi Note",System.currentTimeMillis());

        NOTIFICATION_ID=Integer.parseInt(extras.getString("ID"));
        PendingIntent contentIntent = PendingIntent.getActivity(context,NOTIFICATION_ID,new Intent(context, NotificationReceiver.class), 0);

        String title=extras.getString("title");
        String note=extras.getString("note");

        notification.setLatestEventInfo(context, note, title, contentIntent);
        notification.flags = Notification.FLAG_INSISTENT;
        notification.defaults |= Notification.DEFAULT_SOUND;
        manger.notify(NOTIFICATION_ID, notification);

    }

对于通知集页面

在页面中,按钮用于设置提醒

btnRemind.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                 try
                    {
                     SimpleDateFormat curFormater = new SimpleDateFormat("dd/MM/yy hh:mm aa"); 
                     String s=StartTime;

                    //dateObj1 = curFormater.parse(StartTime); 
                    dateObj1 = curFormater.parse(StartTime);
                    //SimpleDateFormat postFormater = new SimpleDateFormat("dd/MM/yy H:mm:ss.SSS"); 
                    SimpleDateFormat postFormater = new SimpleDateFormat("dd/MM/yy H:mm");
                    Date date2 = postFormater.parse(postFormater.format(dateObj1));

                    Calendar cl=Calendar.getInstance();
                    cl.setTime(date2);

                    MONTH=cl.get(Calendar.MONTH);
                    YEAR=cl.get(Calendar.YEAR);
                    DAY=cl.get(Calendar.DAY_OF_MONTH);
                    HOUR=cl.get(Calendar.HOUR_OF_DAY);
                    if(cl.get(Calendar.MINUTE)>5)
                          cl.add(Calendar.MINUTE, -5);
                    else
                    {
                         cl.add(Calendar.HOUR, -1);
                         cl.add(Calendar.MINUTE, -5);
                    }
                    MINUTE=cl.get(Calendar.MINUTE);

                    }
                    catch (Exception e) 
                    {
                        e.printStackTrace();    
                    }               

                Calendar cal=Calendar.getInstance();

                cal.set(Calendar.MONTH, MONTH);
                cal.set(Calendar.YEAR, YEAR);               
                cal.set(Calendar.DAY_OF_MONTH, DAY);
                cal.set(Calendar.HOUR_OF_DAY, HOUR);
                cal.set(Calendar.MINUTE, MINUTE);

                Intent alarmintent = new Intent(getApplicationContext(), NotificationReceiver.class);
                alarmintent.putExtra("note",txtEvent.getText()+" starts in 5 minutes on channel " +Channel);
                boolean chkReminder=new DatabaseHelper(getApplicationContext()).checkReminders(Channel+" : "+txtEvent.getText().toString(), dateObj1);
                if(chkReminder)
                {
                    Toast.makeText(getApplicationContext(), "Reminder is already Set for this event !!", Toast.LENGTH_LONG).show();
                }
                else
                {
                    //new DatabaseHelper(getApplicationContext()).addReminder(Channel+" : "+txtEvent.getText().toString(), dateObj1);
                    int ID = Integer.parseInt(strEventID);
                    new DatabaseHelper(getApplicationContext()).addReminder(String.valueOf(ID), Channel+" : "+txtEvent.getText().toString(), dateObj1);
                    //Random r = new Random();
                    //int ID = r.nextInt();
                    alarmintent.putExtra("ID", strEventID);
                    PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(),ID,alarmintent,PendingIntent.FLAG_UPDATE_CURRENT|  Intent.FILL_IN_DATA);

                    AlarmManager am = (AlarmManager) getApplicationContext().getSystemService("alarm");
                    am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
                    Toast.makeText(getApplicationContext(), "Reminder Set !!", Toast.LENGTH_LONG).show();
                    //btnRemind.setVisibility(View.INVISIBLE);
                }
            }
        });

    }

在我使用之前删除提醒:

private void checkButtonClick() 
    {
        ImageButton btnDelete=(ImageButton) findViewById(R.id.imgbtnDelete);
        btnDelete.setOnClickListener(new OnClickListener() 
        {

                    public void onClick(View v) 
                    {

                        StringBuffer responseText = new StringBuffer();
                        responseText.append("The following reminder is deleted from the list...\n");
                        String strRmDt="";
                        String strChannel="";
                        ArrayList<States> stateList = dataAdapter.stateList;
                        String strID="";
                        for(int i=0;i<stateList.size();i++)
                        {
                             States state = stateList.get(i);

                            if(state.isSelected())
                            {
                                try
                                {
                                    //String reminder=stateList.get(i).toString();
                                    String reminder=state.name;
                                    String[] separated = reminder.split(" - ");
                                    String[] strSep2 = separated[0].toString().split(" :: ");
                                    new DatabaseHelper(getApplicationContext()).removeReminder(strSep2[0].toString());
                                    // delete notification alarm
                                    @SuppressWarnings("unused")
                                    SimpleDateFormat curFormater = new SimpleDateFormat("dd/MM/yy hh:mm aa"); 

                                    strRmDt=separated[1];

                                    //String stEvent=separated[0].toString();
                                    strID=strSep2[0].toString();
                                    int ID=Integer.parseInt(strID);
                                    Intent I = new Intent(getBaseContext(),NotificationReceiver.class);
                                    I.setAction(String.valueOf(ID));
                                    PendingIntent pIntent = PendingIntent.getBroadcast(getBaseContext(), ID, I, PendingIntent.FLAG_CANCEL_CURRENT);//.getBroadcast(getApplicationContext(), Integer.parseInt(strSep2[0].toString()), getIntent(), PendingIntent.FLAG_CANCEL_CURRENT);
                                    AlarmManager am = (AlarmManager) getApplicationContext().getSystemService("alarm");
                                    am.cancel(pIntent);
                                    pIntent.cancel();
                                    /*
                                    AlarmManager am = (AlarmManager) getSystemService(getApplicationContext().ALARM_SERVICE);
                                    Intent I = new Intent(getApplicationContext(),NotificationReceiver.class);
                                    PendingIntent P = PendingIntent.getBroadcast(getApplicationContext(), Integer.parseInt(strSep2[0].toString()), I, 0);
                                    am.cancel(P);
                                    P.cancel();
                                    */
                                    // end delete notification alarm
                                }
                                catch(Exception e) 
                                {
                                    System.out.print(e.getMessage());
                                    e.printStackTrace();
                                }   
                            }

                        }
                        Toast.makeText(getApplicationContext(),
                        responseText, Toast.LENGTH_LONG).show();
                        onRestart();
                    }
        });

        //RemindersListView.setAdapter(dataAdapter);
    }

伙计们,请帮助我

提前致谢。

0 个答案:

没有答案