安卓AlarmManager:如何避免去掉过去报警 [英] Android AlarmManager: how to avoid to going off of past alarms
问题描述
你好,感谢你的帮助。
我有一个使用AlarmManager每天设定一个报警的下一个星期的应用程序,数月......
- 我为一周的每一天设定一个报警启动活动
和
- 在一个报警为一周的每一天后停止活动 一段时间
我有以下问题,我会尝试在下面几行说明:
今天是周三,
我打开应用程序,并设置我的报警周一,周二,周三,周四,周五,周六,周日... 当我设置了报警:
所有报警的MON和TUE马上去关我结束了活动的4个实例!!!!
请我该如何避免这种情况???
请参见我的一块code:
//设置了报警的启动活动
意图SMON =新的意图(ctxt,VideoActivty.class);
smon.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent psmon = PendingIntent.getActivity(ctxt,0,SMON,0);
日历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);
//设置了报警的查杀活动
意图FMON =新的意图(ctxt,VideoActivty.class);
fmon.putExtra(完成,真正的);
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);
这是该活动:
公共类VideoActivty延伸活动{
私人VideoView视频;
私人的MediaController CTLR;
私人PowerManager.WakeLock WL;
私人KeyguardLock键盘保护;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
//
电源管理器PM =(电源管理器),这
.getSystemService(this.POWER_SERVICE);
WL = pm.newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK,);
wl.acquire();
KeyguardManager公里=(KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
键盘锁= km.newKeyguardLock(MyApp的);
keyguard.disableKeyguard();
。getWindow()和setFormat(PixelFormat.TRANSLUCENT);
VideoView videoHolder =新VideoView(本);
//如果你想控制出现
videoHolder.setMediaController(新的MediaController(本));
乌里视频= Uri.parse(android.resource://+ getPackageName()+/
+ R.raw.ingress); //不添加任何扩展
//如果您的文件被命名为sherif.mp4并放置在/生
//使用R.raw.sherif
videoHolder.setVideoURI(视频)
的setContentView(videoHolder);
videoHolder.start();
videoHolder.setOnCompletionListener(新MediaPlayer.OnCompletionListener(){
@覆盖
公共无效onCompletion(MediaPlayer的MP){
mp.start();
}
});
}
@覆盖
保护无效onNewIntent(意向我){
如果(i.getBooleanExtra(完成,FALSE)){
wl.release();
keyguard.reenableKeyguard();
完();
}
}
}
我们是否有办法解决吗?我仍然在寻找解决办法,我的code如下..
calendar.set(Calendar.DAY_OF_WEEK,getweekday(OBJ));
calendar.set(Calendar.HOUR_OF_DAY,timepicker.getCurrentHour());
calendar.set(Calendar.MINUTE,timepicker.getCurrentMinute());
calendar.set(Calendar.SECOND,0);
calendar.set(Calendar.MILLISECOND,0);
如果(calendar.before(cal_now)){//如果在过去的增量
calendar.add(Calendar.DATE,1);
}
PendingIntent发送= PendingIntent.getBroadcast(getApplicationContext(),_ ID,意图,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager AM =(AlarmManager)getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY * 7,发送方);
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();
}
}
}
Do we have a solution here ? I am still looking for solution, my code as below..
calendar.set(Calendar.DAY_OF_WEEK,getweekday(obj));
calendar.set(Calendar.HOUR_OF_DAY,timepicker.getCurrentHour());
calendar.set(Calendar.MINUTE, timepicker.getCurrentMinute());
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
if(calendar.before(cal_now)){//if its in the past increment
calendar.add(Calendar.DATE,1);
}
PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(),_id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY * 7, sender);
这篇关于安卓AlarmManager:如何避免去掉过去报警的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!