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();
}
}