1

I have written this class to call reciver every 24 hours

I was trying to use same receiver for "SabahReceiver" then i added another receiver "MasaReceiver", all possibilities did not work for me!

anybody can tell me whats going wrong !?

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.mbh.test.receivers.MasaReciever;
import com.mbh.test.receivers.SabahReciever;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by MBH on 17/12/15.
 */
public class SabahMasaAlarmManager {
    public static final int MINUTE = 60000;
    public static final int HOUR = 1000 * 60 * 60;
    public static final int DAY = 86400000;

    private PendingIntent mSabahPendingIntent;
    private PendingIntent mMasaPendingIntent;
    private Context mContext;

    private void SetupAlarms() {
        /* Retrieve a PendingIntent that will perform a broadcast */
        Intent sabahAlarm = new Intent(mContext, SabahReciever.class);
//        sabahAlarm.putExtra(SabahReciever.KEY_IS_SABAH, true);
        mSabahPendingIntent = PendingIntent.getBroadcast(mContext, 100, sabahAlarm, 0);

        /* Retrieve a PendingIntent that will perform a broadcast */
        Intent masaAlarm = new Intent(mContext, MasaReciever.class);
//        sabahAlarm.putExtra(SabahReciever.KEY_IS_SABAH, false);
        mMasaPendingIntent = PendingIntent.getBroadcast(mContext, 101, masaAlarm, 0);
    }

    public void StartAlarm() {

        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss" );

        Date now = Calendar.getInstance().getTime();

        Calendar sabahDate = Calendar.getInstance();
        sabahDate.set(Calendar.HOUR_OF_DAY, 23);
        sabahDate.set(Calendar.MINUTE, 47);
        sabahDate.set(Calendar.SECOND, 0);
//        sabahDate.set(Calendar.HOUR_OF_DAY, 6);
//        sabahDate.set(Calendar.MINUTE, 0);
        if(sabahDate.getTime().before(now)){
            sabahDate.add(Calendar.DAY_OF_YEAR, 1);
        }

        Log.d("DATE", "Sabah: "+ dateFormat.format(sabahDate.getTime()));

        Calendar masaDate = Calendar.getInstance();
        masaDate.set(Calendar.HOUR_OF_DAY, 23);
        masaDate.set(Calendar.MINUTE, 49);
//        masaDate.set(Calendar.HOUR_OF_DAY, 20);
//        masaDate.set(Calendar.MINUTE, 0);
        masaDate.set(Calendar.SECOND, 0);
        if(masaDate.getTime().before(now)){
            masaDate.add(Calendar.DAY_OF_YEAR, 1);
        }


        Log.d("DATE", "Masa: "+ dateFormat.format(masaDate.getTime()));

        AlarmManager manager = (AlarmManager) mContext.getApplicationContext().getSystemService(Context.ALARM_SERVICE);

        manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, sabahDate.getTimeInMillis(), DAY, mSabahPendingIntent);
//        manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+10000, 86400000, mSabahPendingIntent);

        manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, masaDate.getTimeInMillis(), DAY, mMasaPendingIntent);
//        manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+20000, 86400000, mMasaPendingIntent);
    }

    public void cancelAlarm() {
        AlarmManager managerSabah = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
        managerSabah.cancel(mSabahPendingIntent);

        AlarmManager managerMasa = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
        managerMasa.cancel(mMasaPendingIntent);
    }

    public SabahMasaAlarmManager(Context baseContext) {
        mContext = baseContext;
        SetupAlarms();
    }
}
MBH
  • 16,271
  • 19
  • 99
  • 149

1 Answers1

1

setInexactRepeating() is not bound to fire the alarm exactly after the mentioned time. There may be some delay or it may fire a bit early. Basically framework 'batch' the inexact alarms to fire all at one time. Use setExact. Also consider the deep sleep mode. See How to use CPU to perform any operation in deep sleep mode. As you want alarm to fire once in 24 hours, there are good chances that device will be in sleep mode when alarm fires.

Community
  • 1
  • 1
cgr
  • 4,578
  • 2
  • 28
  • 52
  • I tried to keep the device opened and did lots of tests on this, still did not get it fired, now i will try setExact – MBH Dec 18 '15 at 22:23
  • Sure, it will work. Use WakefulReceiver and startWakefulService(). – cgr Dec 18 '15 at 22:26
  • sorry one more question, is there any setExactRepeating or alternative for this ? – MBH Dec 18 '15 at 22:27
  • 1
    No. No repeat for exact alarms. You have to set again when it is fired so it will repeat again. THere is setRepeat() which is not exact but there is no setExactRepeat(). – cgr Dec 18 '15 at 22:29
  • Thank you so much, setRepeating works perfectly for now ! i guess this is what i want, couple of minutes or seconds difference is not important in my situation – MBH Dec 18 '15 at 23:18
  • If delay/early is not a matter, it is good go with inexact alarms. All the best ! :-) – cgr Dec 18 '15 at 23:20
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/98386/discussion-between-mbh-and-cgr). – MBH Dec 18 '15 at 23:22