3

Hi i am new to android i am developing alarm application for that i need to invoke alarm repeatedly for the same time in different days selected by the user. what i meen if i set alarm time as 10AM and repeat option as sunday, monday, saturday the alarm will be invoked on every sunday,monday,saturday.

Here i am using the code to invoke the alarm

AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
                Intent intent = new Intent(Alarm.this, OneShotAlarm.class);  //here i am calling broad \cast receiver to invoke alarm

                 PendingIntent sender = PendingIntent.getBroadcast(Alarm.this, requestCode, intent, 0);
                 Calendar calendar = Calendar.getInstance();
calendar.set(Calender.HOUR_OF_DAY,10); 
                am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);

By using this code i am invoking present day at 10AM.But my gole is need to invoke the alarm for selected days at 10AM.

Here i have another question please help for this also. When i am trying to put the passed time the alarm will be invoked for the current time. How can i give the past time. i mean when i am at 11AP i am need to give 9AM.

Please suggest me if there is any way. Thanks in advance.

raghumudem
  • 747
  • 1
  • 9
  • 17

3 Answers3

7

Please Write below Code in your Broadcast Receiver Class, it will solve your problem.

public class MyBroadcastReceiver extends BroadcastReceiver {
    DBAdapter mDba;
    SQLiteDatabase mDb;
    Ringtone rt;
    MediaPlayer mp;
    AlertDialog.Builder alertbox;
    Context ctx;

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

        DBHelper mDbh = new DBHelper(context, null, null, 1);
        mDb = mDbh.getWritableDatabase();
        mDb.setLockingEnabled(true);
        mDba = new DBAdapter(context);
        mDba.open();
        Cursor cr = mDb.query("mReminderEntry", null, null, null, null,
                null, null);
        if (cr.equals(null)) {
            System.out.println("No Data Found");
        } else {
            Date d = new Date();
            System.out.println("Current Hour is:- " + d.getHours());
            System.out.println("Current Minute is:- " + d.getMinutes());
            Calendar calendar = Calendar.getInstance();
            int day = calendar.get(Calendar.DAY_OF_WEEK);
            String today = null;
            if (day == 2) {
                today = "Monday";
            } else if (day == 3) {
                today = "Tuesday";
            } else if (day == 4) {
                today = "Wednesday";
            } else if (day == 5) {
                today = "Thursday";
            } else if (day == 6) {
                today = "Friday";
            } else if (day == 7) {
                today = "Saturday";
            } else if (day == 1) {
                today = "Sunday";
            }
            System.out.println("Today is:- " + today);

            int system_hour = d.getHours();
            int system_minute = d.getMinutes();
            cr.moveToFirst();
            for (int i = 0; i < cr.getCount(); i++) {
                if (cr.getString(3).equals(system_hour + ":" + system_minute)
                        && cr.getString(1).equals("Daily")) {

                    System.out.println("Matched");
                    Intent scheduledIntent = new Intent(context, MyScheduledActivity.class);
                    scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(scheduledIntent);

                    break;

                } else if (cr.getString(3).equals(
                        system_hour + ":" + system_minute)
                        && cr.getString(1).equals(today)) {

                    Intent scheduledIntent = new Intent(context, MyScheduledActivity.class);
                    scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(scheduledIntent);

                    System.out.println("Matched");

                    break;
                } else {
                    System.out.println("No Matching");
                }
                cr.moveToNext();
            }
        }
        cr.close();
        mDba.close();
    }
}

If U have any issue regarding that then tell me.

Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
1

Dipak Keshariya is right and you need to have some logic.. one logic could be:

  • Use a database to store the alarms.

  • Make a service which gets the latest time from Database.

  • Register an Alarm on that

  • Upon Triggering, Check if the alarm was repeating

  • if yes, then update the alarm time in db. i.e now alarm will be edited for next repeat day.

  • Restart the service

Now if your logic is ok, then you dont need to have an answer of your second question as you wouldn't be needing it.

Farhan
  • 13,290
  • 2
  • 33
  • 59
-2

You can Implement your Repeating alarm in follwing way:

1:First create a database and store your alarms .

2:Rather then using service you can use Time tick reciever(Broad cast Reciever) and in the on recieve method of time tick reciever you can check the current time and current day and match it with the alarm date and time in the database.

3:If the date and time both matches you can trigger the alarm.

Karan_Rana
  • 2,813
  • 2
  • 26
  • 35
  • Is the broad cast receiver checking every time for comparing current time and database time. my application is need to update the time. Is the broad cast receiver onReceiver() method executes every time like services running every time in background. – raghumudem Sep 10 '11 at 08:19
  • 1
    Time tick broad cast receiver runs every minute unlike service which runs all the time consuming the battery life – Karan_Rana Sep 11 '11 at 08:33
  • Avoid using this method since it is unnecessarily expensive solution to have an app constantly checking every minute for an alarm to be triggered. – Jona Feb 06 '13 at 21:47