2

- Ya i know this question has been asked here before and their links are:

Android: Alarm to be play every 30 minutes and it start from 12:30

Using Alarmmanager to start a service at specific time

Android: Alarm to be play every 30 minutes and it start from 12:30

- Now in my case i want my application to be installed at any hour of the day, and then it start ringing every thirty minutes, but NOT from the time of its installation but in the order of the below example...

Eg:

--> I installed the App at 11:15 am.

--> Now as clock of my mobile struck 11:30am it should start ringing, then again at 12:00pm it should ring, then at 12:30pm and so on.....

- Now in my case setRepeating of AlarmManager is not firing, or even if firing its not working....

- I tried using Threads within Service, that worked, but made the whole process to heavy, almost non-functionable.

Here is my code...........

TestService.java

//Its the Service that keeps running.

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.concurrent.TimeUnit;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.widget.Toast;

public class TestService extends Service {
    /*
     * public static SharedPreferences myPrefs; public static
     * SharedPreferences.Editor prefsEditor;
     */
    private PendingIntent pendingIntentFri;
    private boolean ENABLE;
    private boolean isDone = true;
    Intent myIntent;

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        // Toast.makeText(this, "MyAlarmService.onCreate()",
        // Toast.LENGTH_LONG).show();

        // Toast.makeText(this, "Test.onCreate()", Toast.LENGTH_LONG).show();
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        // Toast.makeText(this, "Test.onBind()", Toast.LENGTH_LONG).show();
        return null;
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Toast.makeText(this, "Test.onDestroy()", Toast.LENGTH_LONG).show();
    }

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

        System.out
                .println("I reached TEST SERVICE.........................................................");
        // TODO Auto-generated method stub
        super.onStart(intent, startId);
        // Toast.makeText(this, "Test.onStart()", Toast.LENGTH_LONG).show();
        /*
         * Intent myIntent = new Intent(TestService.this, MyAlarmService.class);
         * pendingIntent = PendingIntent.getService(TestService.this, 0,
         * myIntent, 0);
         */

        // myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
        ENABLE = ClockActivity.myPrefs.getBoolean("enable", false);
        System.out.println("ALARM SERVICE RECREATED WITH THE VALUE :::::: "
                + ENABLE);

        System.out
                .println("FROM TESTSERVICE..........................................:"
                        + ENABLE);

        if (ENABLE) {

            System.out
                    .println("FROM IF ENABLED BLOCK TESTSERVICE..........................................:"
                            + ENABLE);
            startAlarm();
        } else {

            System.out.println("FROM ELSE ITS NOT ENABLED: " + ENABLE);
        }
    }

    public void startAlarm() {
        // AlarmManager alarmManager =
        // (AlarmManager)getSystemService(ALARM_SERVICE);
        myIntent = new Intent(TestService.this, MyAlarmService.class);
        // pendingIntent = PendingIntent.getService(SettingsActivity.this, 0,
        // myIntent, 0);

        // Working for the Perfect Time...
        Calendar cur_cal = new GregorianCalendar();
        cur_cal.setTimeInMillis(System.currentTimeMillis());// set the current
                                                            // time and date for
                                                            // this calendar
        final Calendar cal = new GregorianCalendar();
        // cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
        cal.set(Calendar.HOUR, cur_cal.get(Calendar.HOUR));
        cal.set(Calendar.MINUTE, cur_cal.get(Calendar.MINUTE));
        cal.set(Calendar.SECOND, cur_cal.get(Calendar.SECOND));
        // cal.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND));
        cal.set(Calendar.DATE, Calendar.DATE);
        cal.set(Calendar.MONTH, Calendar.MONTH);

        Intent myIntentFri = new Intent(TestService.this, MyAlarmService.class);
        pendingIntentFri = PendingIntent.getService(TestService.this, 0,
                myIntentFri, 0);
        final AlarmManager alarmManagerFri = (AlarmManager) getSystemService(ALARM_SERVICE);
        // alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
        // pendingIntent2);

        alarmManagerFri.setRepeating(AlarmManager.RTC_WAKEUP,
                cal.getTimeInMillis(), (30 * 60) * 1000, pendingIntentFri);// for
                                                                            // 30
                                                                            // minutes

    }

    @Override
    public boolean onUnbind(Intent intent) {
        // TODO Auto-generated method stub
        // Toast.makeText(this, "MyAlarmService.onUnbind()",
        // Toast.LENGTH_LONG).show();
        return super.onUnbind(intent);
    }

}

MyAlarmService.java

// The file that holds the logic

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.os.SystemClock;
import android.sax.EndTextElementListener;
import android.widget.Toast;

public class MyAlarmService extends Service {
    public static SharedPreferences myPrefs;
    public static SharedPreferences.Editor prefsEditor;
    private boolean SILENCE;

    @Override
    public void onCreate() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

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

        System.out
                .println("I AM IN  MYALARMSERVICE..............................................");
        Calendar ct = Calendar.getInstance();
        ct.setTimeInMillis(System.currentTimeMillis());
        int Hour = ct.get(Calendar.HOUR_OF_DAY);
        int Minute = ct.get(Calendar.MINUTE);
        System.out.println("Hour and Minute in Calender is: " + Hour + ":"
                + Minute);

        myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
        SILENCE = myPrefs.getBoolean("silence", false);

        int sHour = myPrefs.getInt("start_hour", 01);
        int sMinute = myPrefs.getInt("start_minute", 00);
        int eHour = myPrefs.getInt("end_hour", 02);
        int eMinute = myPrefs.getInt("end_minute", 00);

        System.out.println("======== SILENCE PERIOD ==========");
        System.out.println("The Start Time is: " + sHour + ":" + sMinute);
        System.out.println("The END Time is: " + eHour + ":" + eMinute);
        System.out.println("==================================");

        Calendar currentTime = new GregorianCalendar();

        // currentTime.set(Calendar.HOUR_OF_DAY, Calendar.HOUR_OF_DAY);
        // currentTime.set(Calendar.MINUTE, Calendar.MINUTE);
        // currentTime.set(Calendar.SECOND, Calendar.SECOND);
        // currentTime.set(Calendar.DATE, Calendar.DATE);
        // currentTime.set(Calendar.MONTH, Calendar.MONTH);

        Calendar start_time = new GregorianCalendar();
        start_time.set(Calendar.HOUR_OF_DAY, sHour);
        start_time.set(Calendar.MINUTE, sMinute);
        // start_time.set(Calendar.SECOND, Calendar.SECOND);
        // start_time.set(Calendar.DATE, Calendar.DATE);
        // start_time.set(Calendar.MONTH, Calendar.MONTH);

        Calendar end_time = new GregorianCalendar();
        end_time.set(Calendar.HOUR_OF_DAY, eHour);
        end_time.set(Calendar.MINUTE, eMinute);

        // if((sHour>eHour)){ // if the Start == PM and End == AM
        // System.out.println("Day is increase. . . .");
        // end_time.add(Calendar.DATE, 1);
        // // end_time.set(Calendar.SECOND, Calendar.SECOND);
        // // end_time.set(Calendar.DATE, Calendar.DATE);
        // // end_time.set(Calendar.MONTH, Calendar.MONTH);
        // }else{ // if the Start == PM and End == AM
        //
        // System.out.println("Day is same. . . .");
        //
        // // end_time.set(Calendar.SECOND, Calendar.SECOND);
        // // end_time.set(Calendar.DATE, Calendar.DATE);
        // // end_time.set(Calendar.MONTH, Calendar.MONTH);
        // }

        System.out
                .println("=============== CURRENT TIME DETAIL ===============");
        System.out.println("CURRENT TIME:   " + currentTime.getTime());
        System.out
                .println("CURRENT DATE:   " + currentTime.getTime().getDate());
        System.out.println("CURRENT HOUR:   "
                + currentTime.getTime().getHours());
        System.out
                .println("===================================================");
        System.out.println("");
        System.out.println("");
        System.out.println("=============== START TIME DETAIL ===============");
        System.out.println("START DATE:   " + start_time.getTime().getDate());
        System.out.println("START TIME:   " + start_time.getTime());
        System.out.println("START HOUR:   " + start_time.getTime().getHours());
        System.out
                .println("===================================================");
        System.out.println("");
        System.out.println("");
        System.out.println("=============== END TIME DETAIL ===============");
        System.out.println("END TIME:   " + end_time.getTime());
        System.out.println("END DATE:   " + end_time.getTime().getDate());
        System.out.println("END HOUR:   " + end_time.getTime().getHours());
        System.out
                .println("===================================================");
        System.out.println("");
        System.out.println("");

        System.out.println(start_time.getTimeInMillis() + " < "
                + System.currentTimeMillis() + " && "
                + System.currentTimeMillis() + " > "
                + end_time.getTimeInMillis());

        if (!SILENCE) {
            System.out.println("SILENCE in if is :" + SILENCE);
            fireAlarm(Hour, Minute);
        } else {

            System.out.println("SILENCE in else is: " + SILENCE);

            if (sHour > eHour) { // FOR START == PM and END == AM

                System.out.println("FOR start=PM AND end=AM");

                if (((((start_time.getTimeInMillis() <= System
                        .currentTimeMillis())) || ((System.currentTimeMillis() <= (end_time
                        .getTimeInMillis()) + ((3 * 60) * 1000)))))) {
                    System.out.println("You are in Silent mode");
                    Toast.makeText(getApplicationContext(),
                            "Ship's Clock is Silent", Toast.LENGTH_SHORT)
                            .show();
                } else {
                    fireAlarm(Hour, Minute);
                }
            } else { // FOR START == AM/PM and END == AM/PM

                System.out.println("FOR start=AM/PM AND end=AM/PM");

                if (((((start_time.getTimeInMillis() <= System
                        .currentTimeMillis())) && ((System.currentTimeMillis() <= (end_time
                        .getTimeInMillis()) + ((3 * 60) * 1000)))))) {
                    System.out.println("You are in Silent mode");
                    Toast.makeText(getApplicationContext(),
                            "Ship's Clock is Silent", Toast.LENGTH_SHORT)
                            .show();
                } else {
                    fireAlarm(Hour, Minute);
                }
            }

        }
    }

    public void fireAlarm(int Hour, int Minute) {

        // if(Hour>12){
        // Toast.makeText(getApplicationContext(),
        // "Please Set the More the 12 Hour", Toast.LENGTH_SHORT).show();
        // }

        // 12:30
        if ((Hour == 12) && (Minute == 30)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("ONE Bell");
            playSound(R.raw.one_bell);
        }
        // 01:00
        if ((Hour == 1) && (Minute == 00)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("TWO Bell");
            playSound(R.raw.two_bells);
        }
        // 01:30
        if ((Hour == 1) && (Minute == 30)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("THREE Bell");
            playSound(R.raw.three_bells);
        }
        // 02:00
        if ((Hour == 2) && (Minute == 00)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("FOUR Bell");
            playSound(R.raw.four_bells);
        }
        // 02:30
        if ((Hour == 2) && (Minute == 30)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("FIVE Bell");
            playSound(R.raw.five_bells);
        }
        // 03:00
        if ((Hour == 3) && (Minute == 00)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("SIX Bell");
            playSound(R.raw.six_bells);
        }
        // 03:30
        if ((Hour == 3) && (Minute == 30)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("SEVEN Bell");
            playSound(R.raw.seven_bells);
        }
        // 04:00
        if ((Hour == 4) && (Minute == 00)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("EIGHT Bell");
            playSound(R.raw.eight_bells);
        }
        // 04:30
        if ((Hour == 4) && (Minute == 30)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("ONE Bell");
            playSound(R.raw.one_bell);
        }
        // 05:00
        if ((Hour == 5) && (Minute == 00)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("TWO Bell");
            playSound(R.raw.two_bells);
        }
        // 05:30
        if ((Hour == 5) && (Minute == 30)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("THREE Bell");
            playSound(R.raw.three_bells);
        }
        // 06:00
        if ((Hour == 6) && (Minute == 00)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("FOUR Bell");
            playSound(R.raw.four_bells);
        }
        // 06:30
        if ((Hour == 6) && (Minute == 30)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("FIVE Bell");
            playSound(R.raw.five_bells);
        }
        // 07:00
        if ((Hour == 7) && (Minute == 00)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("SIX Bell");
            playSound(R.raw.six_bells);
        }
        // 07:30
        if ((Hour == 7) && (Minute == 30)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("SEVEN Bell");
            playSound(R.raw.seven_bells);
        }
        // 08:00
        if ((Hour == 8) && (Minute == 00)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("EIGHT Bell");
            playSound(R.raw.eight_bells);
        }
        // 08:30
        if ((Hour == 8) && (Minute == 30)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("ONE Bell");
            playSound(R.raw.one_bell);
        }
        // 09:00
        if ((Hour == 9) && (Minute == 00)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("TWO Bell");
            playSound(R.raw.two_bells);
        }
        // 09:30
        if ((Hour == 9) && (Minute == 30)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("THREE Bell");
            playSound(R.raw.three_bells);
        }
        // 10:00
        if ((Hour == 10) && (Minute == 00)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("FOUR Bell");
            playSound(R.raw.four_bells);
        }
        // 10:30
        if ((Hour == 10) && (Minute == 30)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("FIVE Bell");
            playSound(R.raw.five_bells);
        }
        // 11:00
        if ((Hour == 11) && (Minute == 00)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("SIX Bell");
            playSound(R.raw.six_bells);
        }
        // 11:30
        if ((Hour == 11) && (Minute == 30)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("SEVEN Bell");
            playSound(R.raw.seven_bells);
        }
        // 12:00
        if ((Hour == 12) && (Minute == 00)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("Eight Bell");
            playSound(R.raw.eight_bells);
        }
        // 00:00
        if ((Hour == 00) && (Minute == 00)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("Eight Bell");
            playSound(R.raw.eight_bells);
        }
        // 00:30
        if ((Hour == 00) && (Minute == 30)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("One Bell");
            playSound(R.raw.one_bell);
        }
        // //////////////////// FOR 24 HOUR

        // 13:00
        if ((Hour == 13) && (Minute == 00)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("Two Bell");
            playSound(R.raw.two_bells);
        }
        // 13:30
        if ((Hour == 13) && (Minute == 30)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("Three Bell");
            playSound(R.raw.three_bells);
        }
        // 14:00
        if ((Hour == 14) && (Minute == 00)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("Four Bell");
            playSound(R.raw.four_bells);
        }
        // 14:30
        if ((Hour == 14) && (Minute == 30)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("Five Bell");
            playSound(R.raw.five_bells);
        }
        // 15:00
        if ((Hour == 15) && (Minute == 00)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("Six Bell");
            playSound(R.raw.six_bells);
        }
        // 15:30
        if ((Hour == 15) && (Minute == 30)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("Seven Bell");
            playSound(R.raw.seven_bells);
        }
        // 16:00
        if ((Hour == 16) && (Minute == 00)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("Eight Bell");
            playSound(R.raw.eight_bells);
        }
        // 16:30
        if ((Hour == 16) && (Minute == 30)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("One Bell");
            playSound(R.raw.one_bell);
        }
        // 17:00
        if ((Hour == 17) && (Minute == 00)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("Two Bell");
            playSound(R.raw.two_bells);
        }
        // 17:30
        if ((Hour == 17) && (Minute == 30)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("Three Bell");
            playSound(R.raw.three_bells);
        }
        // 18:00
        if ((Hour == 18) && (Minute == 00)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("Four Bell");
            playSound(R.raw.four_bells);
        }
        // 18:30
        if ((Hour == 18) && (Minute == 30)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("Five Bell");
            playSound(R.raw.five_bells);
        }
        // 19:00
        if ((Hour == 19) && (Minute == 00)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("Six Bell");
            playSound(R.raw.six_bells);
        }
        // 19:30
        if ((Hour == 19) && (Minute == 30)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("Seven Bell");
            playSound(R.raw.seven_bells);
        }
        // 20:00
        if ((Hour == 20) && (Minute == 00)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("Eight Bell");
            playSound(R.raw.eight_bells);
        }
        // 20:30
        if ((Hour == 20) && (Minute == 30)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("One Bell");
            playSound(R.raw.one_bell);
        }
        // 21:00
        if ((Hour == 21) && (Minute == 00)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("two Bell");
            playSound(R.raw.two_bells);
        }
        // 21:30
        if ((Hour == 21) && (Minute == 30)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("Three Bell");
            playSound(R.raw.three_bells);
        }
        // 22:00
        if ((Hour == 22) && (Minute == 00)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("Four Bell");
            playSound(R.raw.four_bells);
        }
        // 22:30
        if ((Hour == 22) && (Minute == 30)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("Five Bell");
            playSound(R.raw.five_bells);
        }
        // 23:00
        if ((Hour == 23) && (Minute == 00)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("Six Bell");
            playSound(R.raw.six_bells);
        }
        // 23:30
        if ((Hour == 23) && (Minute == 30)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("Seven Bell");
            playSound(R.raw.seven_bells);
        }
        // 24:00
        if ((Hour == 24) && (Minute == 00)) {
            System.out.println("The Time is: " + Hour + " : " + Minute);
            System.out.println("Eight Bell");
            playSound(R.raw.eight_bells);
        }
    }

    @Override
    public boolean onUnbind(Intent intent) {
        // TODO Auto-generated method stub
        // Toast.makeText(this, "MyAlarmService.onUnbind()",
        // Toast.LENGTH_LONG).show();
        return super.onUnbind(intent);
    }

    public void playSound(int resources) {

        MediaPlayer mp = MediaPlayer.create(getApplicationContext(), resources);
        mp.start();

    }

}
Community
  • 1
  • 1
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
  • 1
    Please, this is way too long, snip out the repetitive stuff and leave only what is strictly necessary to ask the question! – markus Jan 24 '13 at 22:58
  • Thereof redundancy that you can simplify with some smarter logic handling. – JoxTraex Jan 25 '13 at 00:28

2 Answers2

2

Well, this is pretty broken:

    cal.set(Calendar.DATE, Calendar.DATE); <-- This sets the day of the month to 5!
    cal.set(Calendar.MONTH, Calendar.MONTH); <-- This sets the month to 2 (March)!

What are you trying to do here?

Why don't you just create a Calendar with the parameters you want, like this:

Calendar cal = new GregorianCalendar(); // Current date and time
cal.set(Calendar.MILLISECOND, 0);
cal.set(Calendar.SECOND, 0);
// Round minutes up to next multiple of 30
int minutes = cal.get(Calendar.MINUTE);
if (minutes < 30) {
    cal.set(Calendar.MINUTE, 30);
} else {
    cal.set(Calendar.MINUTE, 0);
    cal.add(Calendar.HOUR, 1); // Next hour (will roll the day/month/year over if needed)
}
David Wasser
  • 93,459
  • 16
  • 209
  • 274
1

Another way to do it is to take the current time and add 30 mins in ms:

30 mins (30 mins to ms = 60s * 1000ms * 30) = 1800000 ms

long Offset = Calendar.getInstance().getTimeInMillis() + 1800000;

And you should be printing what time the alarm is set for DURING debugging to ensure it is set to go off at the right time.

use the getTime() method on the Calendar Instance and then print it in local time.

JoxTraex
  • 13,423
  • 6
  • 32
  • 45