1

こんにちは、ご協力ありがとうございます。

AlarmManager を使用して、平日のアクティビティを開始および停止しようとしています。

今、私は理解できないこの問題に直面しています。

曜日ごとに、2 つの PendingIntents を作成します。

  • 活動を開始するための 1 つ
  • アクティビティを殺すためのもの

以下のコードを見ることができます。

問題は次のとおりです。

- 2 番目の PendingIntent のみが起動します

(アクティビティを開始すると、後で停止する必要がありますが、最初の PendingIntent がオフにならないため、2 番目の 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);

保留中の 2 番目の意図の目的は

アクティビティを伝える (最初の保留中の意図によって開始)

自分自身を終わらせるために。

(

総括する:

アクティビティを開始する最初の PendingIntent、

終了するすべてのアクティビティに対する 2 番目の PendingIntent

)

アクティビティのコードは次のとおりです。

 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.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ì");

そして以下のメソッドをonCreateof で呼び出します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 に答える