-1

I am implementing a sample code for BroadcastReceiver, where it will start from a specific time, but there is something problem in my code and its not working..

Here is my code:

In Activity:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Calendar cur_cal = new GregorianCalendar(TimeZone.getTimeZone("IST"), Locale.US);
        cur_cal.setTimeInMillis(System.currentTimeMillis());

        Calendar cal = new GregorianCalendar();
        cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
        cal.add(Calendar.HOUR_OF_DAY, 13);
        cal.add(Calendar.MINUTE, 10);
        cal.add(Calendar.SECOND, 00);
        cal.add(Calendar.MILLISECOND, 0);
        cal.add(Calendar.DATE, 31);
        cal.add(Calendar.MONTH, 10);

        Intent intent = new Intent(HelloAlarmManagerActivity.this, ScheduleHandler.class);
        PendingIntent pIntent = PendingIntent.getBroadcast(HelloAlarmManagerActivity.this, 0, intent, 0);

        AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 2*10, pIntent);
        Toast.makeText(HelloAlarmManagerActivity.this, "Alarm seted", Toast.LENGTH_SHORT).show();

    }

and my BrodcastReceiver class is as :

public class ScheduleHandler extends BroadcastReceiver {

    /* (non-Javadoc)
     * @see android.content.BroadcastReceiver#onReceive(android.content.Context, android.content.Intent)
     */
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Toast.makeText(context, "Alarm called", Toast.LENGTH_SHORT).show();
    }


}

After running this application, I am getting only first toast message. It means broadcast receiver not getting called.

Is there any problem in my code?

2
  • just an idea: can it be that if you call cal.add(Calendar.Month, 10) that you added 10 Month to NOW? Try to determine what a value is cal.getTimeInMillis. You could also try alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis + 1000, 2*10, pIntent); to see if its a simple timing mistake
    – Rafael T
    Oct 31, 2011 at 8:46
  • @Rafael T cal.add(Calendar.Month, 10) is this mean I am adding 10 months from now? Ohhh I thought it should be of Oct then I added 10 (for Oct).... so what should be for current month? Oct 31, 2011 at 8:51

1 Answer 1

1

It looks like your Repeat interval is 20 (2*10) milliseconds. I have no Idea how phones would react to that, but It might be the cause of your problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.