1

您好,感谢您的帮助。

请我尝试使用 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();
  }
}

}

4

1 回答 1

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 的两个意图。

请注意,该活动将在现有活动的上下文之外启动,因此您必须在 Intent 中使用 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);
    }

这将起作用

于 2013-07-11T11:07:18.320 回答