Android处理ProgressBar onPause / onResume的状态

时间:2014-05-31 09:18:27

标签: android

你有一个我跑的进度棒。但是当用户通过主页按钮退出应用程序,然后通过按住主页按钮并选择应用程序重新输入它。 progressBar setProgress在旧值和新值之间闪烁,几乎就像它记住前一个状态一样。我已经在我的on pause / onResume方法中创建了一个函数来暂停计时器保存值,并在恢复时获取这些值并再次启动计时器和进度条。令人困惑的是,如果运行onPause函数并且用户停留在应用程序中,例如按下后退按钮,则onPause内容可以正常工作。有谁知道我哪里出错或如何使用CountDownTimer正确实现progressBar?

继承我的代码

@Override
    protected void onResume() {
        // TODO Auto-generated method stub
        Log.v("Game","ONRESUME");

        loadData();
        layoutChecks();     
        loadViews();
        try {
            if(!myData.get("completed").equals("3")){
                hintCheck();
                getHints();
                startTimer();
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        super.onResume();
    }


    @Override
    protected void onPause() {
        Log.v("Game","ONPAUSE");
        try {
            if(!myData.get("completed").equals("3")){

                pauseTimer();
                updateData();

            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        super.onPause();

    } 



public void loadViews(){
  progressBar = new ProgressBar(this);
                progressBar = (ProgressBar) findViewById(R.id.progressBar);
                progressD = progressBar.getProgressDrawable();
}


public void prepareProgressBar(){
        progressD.setLevel(0);
        progressBar.setProgress(0);
        progressD.setLevel(points*100);
        progressBar.setProgress(points);
    }

    public void startTimer(){
        try {
         points = Integer.valueOf(myData.getJSONObject("Data").getString("points"));
         int timer = points;
         hasTimerCancelled = false;
         prepareProgressBar();
         counter = new MyCount(timer/2 * 1000,1000);
         counter.start(); 

        } catch (NumberFormatException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (JSONException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

    }

    public void pauseTimer(){

        try {
            counter.cancel();
            hasTimerCancelled = true;
            JSONObject Data = myData.getJSONObject("Data");
            Data.put("points", String.valueOf(points));
            myData.put("Data", Data);
            updateData();

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }


    public class MyCount extends CountDownTimer{

        public long totaltime; 

        public MyCount(long totalTime, long countDownInterval) {
            super(totalTime, countDownInterval);
        totaltime = totalTime;

        }

        @Override
        public void onFinish() {
        counter.cancel();
        points = 0;
        progressBar.setProgress(0); 



        }

        @Override
        public void onTick(long millisUntilFinished) {


            if(!hasTimerCancelled){
                if(points > 0){

                 points = points - 1;

                }

                progressBar.setProgress((int) totaltime);   

            }

        }

    }

0 个答案:

没有答案