你好,感谢你的帮助。
我有一个应用程序,使用AlarmManager为未来几周、几个月每天设置一个闹钟。
- 我为每一周的每一天设置了一个启动Activity的闹钟。
还有
- 我为每一周的每一天设置了一个在一段时间后关闭Activity的闹钟。
我遇到了以下问题,我将在下面的行中尝试解释:
今天是星期三,
我打开应用程序并设置了MON、TUE、WED、THU、FRI、SAT、SUN的闹铃...当我设置了闹铃后:
所有MON和TUE的闹钟立即响起,我最终有4个Activity的实例!
请问如何避免这种情况?
请看我的代码片段:
// 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);
这是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();
}
}
}
Calendar
对象的时区更改为UTC。 - Joel Bodega