您好,感谢您的帮助。
请我尝试使用 AlarmManager 在一周内开始和停止活动。
现在我遇到了这个我不明白的问题。
对于一周中的每一天,我创建 2 个 PendingIntents:
- 一个用于开始活动
- 一个用于杀死活动
你可以看到下面的代码。
现在的问题是:
- 只有第二个 PendingIntent 触发
(并启动活动,理论上它应该停止它,但由于第一个 PendingIntent 永远不会关闭,第二个 PendingIntent 启动活动而不是关闭它)。
- 显然第一个 PendingIntent 永远不会发生!!!!
/ INTENT THAT SHOULD START THE ACTIVITY
Intent sthu = new Intent(ctxt, VideoActivty.class);
PendingIntent psthu = PendingIntent.getActivity(ctxt, 0, sthu, 0);
Calendar calSet7 = Calendar.getInstance();
calSet7.set(Calendar.MONTH, c.get(Calendar.MONTH));
calSet7.set(Calendar.YEAR, c.get(Calendar.YEAR));
calSet7.set(Calendar.DAY_OF_WEEK, 5);
calSet7.set(Calendar.HOUR_OF_DAY, hsthu);
calSet7.set(Calendar.MINUTE, msthu);
calSet7.set(Calendar.SECOND, 0);
calSet7.set(Calendar.MILLISECOND, 0);
mgr.setRepeating(AlarmManager.RTC_WAKEUP, calSet7.getTimeInMillis(),
7 * 24 * 60 * 60 * 1000, psthu);
// INTENT THAT SHOULD KILL THE ACTIVITY
Intent fthu = new Intent(ctxt, VideoActivty.class);
fthu.putExtra("finish", true);
PendingIntent pfthu = PendingIntent.getActivity(ctxt, 0, fthu, 0);
Calendar calSet8 = Calendar.getInstance();
calSet8.set(Calendar.MONTH, c.get(Calendar.MONTH));
calSet8.set(Calendar.YEAR, c.get(Calendar.YEAR));
calSet8.set(Calendar.DAY_OF_WEEK, 5);
calSet8.set(Calendar.HOUR_OF_DAY, hfthu);
calSet8.set(Calendar.MINUTE, mfthu);
calSet8.set(Calendar.SECOND, 0);
calSet8.set(Calendar.MILLISECOND, 0);
mgr.setRepeating(AlarmManager.RTC_WAKEUP, calSet8.getTimeInMillis(),
7 * 24 * 60 * 60 * 1000, pfthu);
建议更改后:
显然现在只有第一个 PendingIntent 熄灭:
Intent sthu = new Intent(ctxt, VideoActivty.class);
sthu.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent psthu = PendingIntent.getActivity(ctxt, 1, sthu, 0);
Calendar calSet7 = Calendar.getInstance();
calSet7.set(Calendar.MONTH, c.get(Calendar.MONTH));
calSet7.set(Calendar.YEAR, c.get(Calendar.YEAR));
calSet7.set(Calendar.DAY_OF_WEEK, 5);
calSet7.set(Calendar.HOUR_OF_DAY, hsthu);
calSet7.set(Calendar.MINUTE, msthu);
calSet7.set(Calendar.SECOND, 0);
calSet7.set(Calendar.MILLISECOND, 0);
//calSet.setTimeZone(TimeZone.getTimeZone("UTC"));
mgr.setRepeating(AlarmManager.RTC_WAKEUP, calSet7.getTimeInMillis(),
7 * 24 * 60 * 60 * 1000, psthu);
Log.e("","setto alarm per giovedì");
// INTENT
Intent fthu = new Intent(ctxt, VideoActivty.class);
fthu.putExtra("finish", true);
PendingIntent pfthu = PendingIntent.getActivity(ctxt, 2, fthu, 0);
Calendar calSet8 = Calendar.getInstance();
calSet8.set(Calendar.MONTH, c.get(Calendar.MONTH));
calSet8.set(Calendar.YEAR, c.get(Calendar.YEAR));
calSet8.set(Calendar.DAY_OF_WEEK, 5);
calSet8.set(Calendar.HOUR_OF_DAY, hfthu);
calSet8.set(Calendar.MINUTE, mfthu);
calSet8.set(Calendar.SECOND, 0);
calSet8.set(Calendar.MILLISECOND, 0);
mgr.setRepeating(AlarmManager.RTC_WAKEUP, calSet8.getTimeInMillis(),
7 * 24 * 60 * 60 * 1000, pfthu);
第二个未决意图的目的是
讲述活动(由第一个未决意图开始)
完成它自己。
(
总结一下:
第一个 PendingIntent 启动 Activity,
2nd PendingIntent to all the Activity to finish
)
这是活动的代码:
public class VideoActivty extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
VideoView videoHolder = new VideoView(this);
Uri video = Uri.parse("android.resource://" + getPackageName() + "/"
+ R.raw.ingress);
videoHolder.setVideoURI(video);
setContentView(videoHolder);
videoHolder.start();
@Override
protected void onNewIntent (Intent i){
//HERE I TRY TO TELL THE ACTIVITY (VIA THE SECOND INTENT) TO SHUT DOWN
if( i.getBooleanExtra("finish",false) ){
finish();
}
}
}