简体   繁体   中英

Setting alarm notifications with toggle button - Android

I have a togglebutton in the main activity which enables alarm. aid is a unique key. I need to have an alarm notification when the togglebutton is enabled so on that particular date the alarm will go. When the toggle button is offed the alarm should be canceled. When the togglebutton is on the toast is not coming. which means the BroadcastReceiver is not called. But when the toggle is offed the toast comes.

btnToggle.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {


                if (btnToggle.isChecked()) {


                    AddReminder reminder = new AddReminder(year,
                            month, day, hour,
                            min, body, context, aid);
                    reminder.setNotification();

                } else {
                    AddReminder reminder = new AddReminder(context, mtchid);
                    reminder.offNotification();
                }
            }
        });

This is the AddReminder Class I coded

public class AddReminder {

    private String formattedYear;
    private String formattedMonth;
    private String formattedDay;
    private String formattedHour;
    private String formattedMin;
    private String body;
    private Context context;
    private String aid;
    private int mYear;
    private int mMonth;
    private int mDay;
    private int mHour;
    private int mMin; 
    private int alrmId = 0;
    int aID;

    public AddReminder(Context c, String aid){
        this.context = c;
        this.aid = aid;
        aID = Integer.parseInt(aid);
    }

    public AddReminder(String year, String month, String day, String hour, String min, String body, Context c,  String aid){
        this.formattedYear = year;
        this.formattedMonth = month;
        this.formattedDay = day;
        this.formattedHour = hour;
        this.formattedMin = min;
        this.body = body;
        this.context = c;
        this.aid = aid;
        aID = Integer.parseInt(aid);
    }


    public void setNotification(){

        Calendar c = Calendar.getInstance();
        mYear = Integer.parseInt(formattedYear);
        mMonth = Integer.parseInt(formattedMonth);
        mDay = Integer.parseInt(formattedDay) - 1;
        mHour = Integer.parseInt(formattedHour);
        mMin = Integer.parseInt(formattedMin);


        //set Reminder time and date into calendar object               
        c.set(Calendar.YEAR,mYear);
        c.set(Calendar.MONTH, mMonth);
        c.set(Calendar.DATE, mDay);// 1 days before
        c.set(Calendar.HOUR_OF_DAY, mHour);             
        c.set(Calendar.MINUTE, mMin);
        c.set(Calendar.SECOND, 0);
        alrmId = Integer.parseInt(mMonth + "" + mDay + "" + mHour + ""
                + mMin);
        Intent in = new Intent(context, ReminderReceiver.class);
        in.putExtra("body", body);
        in.putExtra("AlrmId", alrmId);
        in.putExtra("aid",aid);


        PendingIntent sender = PendingIntent.getBroadcast(context, aID, in, PendingIntent.FLAG_CANCEL_CURRENT);
        // Get the AlarmManager service
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), sender);

    }

    public void offNotification(){
        Intent intent = new Intent(context, ReminderReceiver.class);

        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        PendingIntent pi = PendingIntent.getBroadcast(context, aID, intent,0);
        am.cancel(pi);


    }
}

This is the broadcast Receiver class

public class ReminderReceiver extends BroadcastReceiver {

        String body;
        String AlrmId;
        String id;
        String aid;


        @Override
         public void onReceive(Context context, Intent intent) {


             body = intent.getStringExtra("body");
             AlrmId = intent.getStringExtra("AlrmId");

             aid = intent.getStringExtra("aid");


             Toast.makeText(context, body+"/"+AlrmId+"/"+id+"/"+aid, Toast.LENGTH_SHORT).show();


         }
    }

The Toast is not shown because it´s not the time for it. The BroadcastReceiver is reached when the alarmManager fires the message at the given time. So to make the simple message when the alarm is turned on, do it inside the setNotification() event:

     public void setNotification(){

       //Your code
                .
                .
                .
       Toast.makeText(context,"ALARM IS ON", Toast.LENGTH_SHORT().show();
     }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM