如何暂停/恢复处理程序/ runnable?

时间:2015-04-28 15:20:50

标签: java android timer handler runnable

我想按一个按钮,让按钮触发一个定时器,每秒增加一个变量。当我关闭应用程序时,我希望增加暂停,当应用程序打开时,我希望它从最后的位置恢复。我需要做什么?

这是我的代码:

public class MainActivity extends ActionBarActivity {

    public void save(){
        Editor editor = pref.edit();
        editor.putInt("countertest", counter);
        editor.commit();
    }//end of save


    public void read(){
        counter = pref.getInt("countertest", counter);
    }//end of read


      Handler testHandler = new Handler();
      int counter;
      TextView testView;
      Button testButton;
      Runnable Runnable0;
      SharedPreferences pref;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        testView = (TextView) findViewById(R.id.testView);
        testButton = (Button) this.findViewById(R.id.testButton);
        pref = PreferenceManager.getDefaultSharedPreferences(this);



      read();
        testView.setText(Integer.toString(counter));




    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


@Override
protected void onResume() {


    read();


   final Runnable Runnable0 = new Runnable() {
        @Override
        public void run() {
            counter += 1;   
            testView.setText(Integer.toString(counter));
            testHandler.postDelayed(this, 1000);
            save();
            }// end of if   
    };




    /* button click */
    testButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            /* Start runnable */

                testButton.setEnabled(false);
                counter -= 5;
                save();
                testView.setText(Integer.toString(counter));
                testHandler.post(Runnable0);



        }// end of onClick
    });// end of blingButton onClickListener    

    super.onResume();

}//end of onResume

@Override
protected void onPause() {
    save();
    testHandler.removeCallbacks(Runnable0);
    super.onPause();
}//end of onPause
}

1 个答案:

答案 0 :(得分:0)

您应该将上次调用Runnable的时间(以毫秒为单位)存储在这样的变量中:

int lastRunnableCall = Calendar.getInstance().getTimeInMillis();

然后在调用onPause时将当前时间存储在变量中。像这样:

int onPauseTime = Calendar.getInstance().getTimeInMillis();

onResume中,如果您在暂停时间存储了值,则可以计算出正确的延迟时间。

int postDelayedTime = onPauseTime > 0 ? 1000 - (onPauseTime - lastRunnableCall) : 1000; 

如果不尝试这个,我不知道它会有多准确。但这是我要开始的地方