3

私は完全に機能するカウントダウンタイマーを設計していますが、これにとどまる場合に限りActivityます。カウントダウンが開始され、たとえば戻って other を開始するActivityと、 countdonws はカウントダウンに従います。論理的には、カウントダウン タイマーがあるアクティビティをもう一度開始すると、何もカウント ダウンしていないように見えますが、バクグラウンドではカウント ダウンしていることがわかります。タイマーのオンティックで、続行通知をスローして、通知タブ。

コードは次のとおりです。sendnotification メソッドは、時間が終了するとアラートをスローし、sendnotificationTiempo1 秒ごとにタブに通知を送信して上部に表示するメソッドです。

public static final int NOTIFICATION_ID = 33;
public static final int NOTIFICATION_ID_Tiempo = 34;
private int minCrono;
TextView text;
MyCounter timer;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    StartCrono = (Button) findViewById(R.id.butt_start);
    CancelCrono = (Button) findViewById(R.id.butt_cancel);
    text=(TextView)findViewById(R.id.text_countTime1);

    CancelCrono.setEnabled(false);


    minCrono = mins.getCurrentItem();

    StartCrono.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            minCrono=(60000*minCrono);      
            timer = new MyCounter(minCrono,1000);
            timer.start();
        }
    });

    CancelCrono.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            timer.cancel();     
            text.setText("El tiempo se ha cancelado");
            minutosCrono =  mins.getCurrentItem();

            if (mNotificationManager != null)
                mNotificationManager.cancel(NOTIFICATION_ID_Tiempo);

        }           
    });
}

カウントダウンクラス:

public class MyCounter extends CountDownTimer{          
    public MyCounter(long millisInFuture, long countDownInterval) {     
        super(millisInFuture, countDownInterval);           
    }
     @Override
    public void onFinish() {

        StartCrono.setEnabled(true);
        CancelCrono.setEnabled(false);
        mins.setEnabled(true);
        minCrono = mins.getCurrentItem(); 
        // 

        text.setText("Time Complete!"); 

        sendnotification("Guau", "Time finished.");

    }
    @Override
     public void onTick(long millisUntilFinished) {
        long segRestantesTotal=(millisUntilFinished/1000);
        long minRestantes= (segRestantesTotal/60);
        long segRestantes= (segRestantesTotal%60);


        if(segRestantes>=10){
            text.setText(minRestantes+":"+segRestantes);
            sendnotificationTiempo("Guau", minRestantes+":"+segRestantes);
        }
        else{
            text.setText(minRestantes+":0"+segRestantes);
            sendnotificationTiempo("Guau", minRestantes+":0"+segRestantes);
        }
    }   
}   

}

だから私がやるべきことは、いつ戻ってActivity別のものを開始するかを知り、バックグラウンドでカウントダウンしている時間を表示して、たとえばテキストビューで表示することです。

または、通知タブからカウントダウンされている時間を取得できれば完璧です。ありがとう!

4

1 に答える 1

0

MyCounter インスタンスをアクティビティの外に移動します。アクティビティが再起動すると、インスタンスが再作成されるため、古いインスタンスはなくなります。これを行うにはいくつかの方法がありますが、MyCounter インスタンスを、Activity がアクセスできる場所に配置する必要がありますが、そのライフサイクルには関与しません。私が作成した初歩的な ServiceLocator 実装を使用していますが、カスタム Application クラスを簡単に作成して、そこにタイマー (またはタイマーを作成および制御するコントローラー クラス) を保持できます。

于 2012-02-28T17:04:28.007 に答える