Hi and thank you for your help.
I have an application that uses AlarmManager to set one alarm a day for the next weeks, months...
- I set one alarm for each day of the week to start the Activity
and
- one alarm for each day of the week for stopping the Activity after some time
I have the following problem that I will try to explain in the following lines:
Today is Wednesday,
I open the application and set my alarms for MON, TUE, WED, THU, FRI, SAT, SUN... as soon as I set the alarms:
ALL the alarms for MON and TUE go immediately off I end up with 4 instances of the Activity !!!!
Please how do I avoid this???
Please see a piece of my code:
// SET THE ALARM FOR STARTING THE ACTIVITY
Intent smon = new Intent(ctxt, VideoActivty.class);
smon.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent psmon = PendingIntent.getActivity(ctxt, 0, smon, 0);
Calendar calSet = Calendar.getInstance();
calSet.set(Calendar.DAY_OF_WEEK, 2);
calSet.set(Calendar.HOUR_OF_DAY, hsmon);
calSet.set(Calendar.MINUTE, msmon);
calSet.set(Calendar.SECOND, 0);
calSet.set(Calendar.MILLISECOND, 0);
mgr.setRepeating(AlarmManager.RTC_WAKEUP, calSet.getTimeInMillis(),
7 * 24 * 60 * 60 * 1000, psmon);
// SET THE ALARM FOR KILLING THE ACTIVITY
Intent fmon = new Intent(ctxt, VideoActivty.class);
fmon.putExtra("finish", true);
PendingIntent pfmon = PendingIntent.getActivity(ctxt, 0, fmon, 0);
calSet.set(Calendar.DAY_OF_WEEK, 2);
calSet.set(Calendar.HOUR_OF_DAY, hfmon);
calSet.set(Calendar.MINUTE, mfmon);
calSet.set(Calendar.SECOND, 0);
calSet.set(Calendar.MILLISECOND, 0);
mgr.setRepeating(AlarmManager.RTC_WAKEUP, calSet.getTimeInMillis(),
7 * 24 * 60 * 60 * 1000, pfmon);
This is the Activity:
public class VideoActivty extends Activity {
private VideoView video;
private MediaController ctlr;
private PowerManager.WakeLock wl;
private KeyguardLock keyguard;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//
PowerManager pm = (PowerManager) this
.getSystemService(this.POWER_SERVICE);
wl = pm.newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK, "");
wl.acquire();
KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
keyguard = km.newKeyguardLock("MyApp");
keyguard.disableKeyguard();
getWindow().setFormat(PixelFormat.TRANSLUCENT);
VideoView videoHolder = new VideoView(this);
//if you want the controls to appear
videoHolder.setMediaController(new MediaController(this));
Uri video = Uri.parse("android.resource://" + getPackageName() + "/"
+ R.raw.ingress); //do not add any extension
//if your file is named sherif.mp4 and placed in /raw
//use R.raw.sherif
videoHolder.setVideoURI(video);
setContentView(videoHolder);
videoHolder.start();
videoHolder.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mp.start();
}
});
}
@Override
protected void onNewIntent (Intent i){
if( i.getBooleanExtra("finish",false) ){
wl.release();
keyguard.reenableKeyguard();
finish();
}
}
}