嗨,谢谢你的帮助。
请我尝试使用AlarmManager在一周内开始和停止活动。
现在我遇到了这个我不明白的问题。
对于一周中的每一天,我创建了2个PendingIntents:
你可以看到下面的代码。
现在的问题是:
- 只有第二个PendingIntent触发
(并启动Activity,因此它应该停止它,但由于第一个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的目的是
宣传活动(由第一次开始提供)
完成自己。
(适用
总结一下:
启动活动的第一个PendingIntent,
第二个PendingIntent to 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();
}
}
}
答案 0 :(得分:6)
更改PendingIntent(任何一个)的请求代码。他们不应该是一样的。
喜欢
PendingIntent psthu = PendingIntent.getActivity(ctxt, 1 /*Changed Here*/, sthu, 0);
PendingIntent pfthu = PendingIntent.getActivity(ctxt, 2 /*Changed Here*/, fthu, 0);
将Intent.FLAG_ACTIVITY_NEW_TASK
添加到要传递给每个PendingIntents的两个意图中。
请注意,活动将在a的上下文之外启动 现有活动,因此您必须使用Intent.FLAG_ACTIVITY_NEW_TASK 在意图中启动标志。
详细了解getActivity
对我有用的是......
仅针对启动活动注册警报
AlarmManager mgr = (AlarmManager)(getSystemService( Context.ALARM_SERVICE ));
Intent sthu = new Intent(getApplicationContext(), VideoActivty.class);
sthu.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
sthu.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent psthu = PendingIntent.getActivity(getApplicationContext(), 1, sthu, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar startWhen = new GregorianCalendar();
startWhen.add(Calendar.MILLISECOND, 1000);
//calSet.setTimeZone(TimeZone.getTimeZone("UTC"));
mgr.setRepeating(AlarmManager.RTC_WAKEUP, startWhen.getTimeInMillis(),
60 * 1000, psthu);
Log.e("","setto alarm per giovedì");
并在onCreate
VideoActivty
处调用以下方法
private void registerActivityCloseAlarm() {
AlarmManager mgr = (AlarmManager)(getSystemService(Context.ALARM_SERVICE ));
Intent fthu = new Intent(getApplicationContext(), VideoActivty.class);
fthu.putExtra("finish", true);
fthu.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
fthu.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pfthu = PendingIntent.getActivity(getApplicationContext(), 2, fthu, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar endWhen = new GregorianCalendar();
endWhen.add(Calendar.MILLISECOND, 2000);
mgr.set(AlarmManager.RTC_WAKEUP, endWhen.getTimeInMillis(), pfthu);
}
这将有效