为什么每当活动重新启动时进度条都会加速

时间:2014-08-19 13:47:18

标签: android performance android-intent android-activity

我对进度条有疑问。有问题和答案的活动。当用户点击其中一个答案时。活动自称并生成带有答案的新问题。在标题中,我有进度条,进度条已满N秒。每次活动自行调用时,进度条已满,持续N / 2秒。我希望进度条充满N秒,而不是N / 2。如果你能帮助我,我会感激不尽。问题出在curiProgress函数中。这是我的代码。

{
public class beze2 extends Activity{

    String FILENAME = "HighScore";
    FileOutputStream fos;
    private boolean tocan = false;
    private int c, progressStatus=400, id_odgovor, rezultat;
    private myCountDownTimer timer;
    private static int myProgress=400;
    private ProgressBar progressBar;
    private Handler myHandler=new Handler();
    private TextView text, t;
    private int[] b;
    private String d, f, strin;
    private ArrayList numbers;
    private ArrayList<Pitanja> pitanja; 
    private ArrayList<Odgovor> odgovori;
    private TestAdapter mDbHelper;
    private Random r;
    private LinearLayout ln;
    LinearLayout.LayoutParams buttonParams;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getIntents();
        initializeVariables();
        dodajVrijednostiUb();
        postaviPitanje();
        postaviOdgovore();
        curiProgress();
    }

    private void getIntents(){

        Intent i = getIntent();
        String reza = i.getStringExtra("REZULTAT");
        strin = i.getStringExtra("EXTRA_M");
        rezultat = Integer.parseInt(reza);

    }



    private void initializeVariables(){

        progressBar=(ProgressBar)findViewById(R.id.myProgress);
        text = (TextView) findViewById(R.id.vrijeme);
        timer = new myCountDownTimer(20*1000,1*1000);
        numbers = new ArrayList();
        mDbHelper = new TestAdapter(this);         
        mDbHelper.createDatabase();       
        mDbHelper.open();
        pitanja = mDbHelper.getPitanje(Integer.parseInt(strin));
        b = new int[pitanja.size()];
        t = (TextView) findViewById(R.id.text_pitanja);
        myProgress=400;
        r = new Random();
        progressBar.setMax(400);
        ln = (LinearLayout) findViewById(R.id.layoutOdgovori);
        buttonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

    }

    private void curiProgress(){

        new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                while(progressStatus>=0)
                {
                    progressStatus=performTask();
                    myHandler.post(new Runnable()
                    {
                    public void run() {
                    progressBar.setProgress(progressStatus);
                    }
                    });

                }
                myHandler.post(new Runnable() {

                    @Override
                    public void run() {
                       progressStatus=0; 
                       myProgress=0;

                    }
                });

            }
            private int performTask()
            {
                try {
                    //---Do some task---
                    Thread.sleep(50);
                    } catch (InterruptedException e)
                    {
                    e.printStackTrace();
                    }
                    return --myProgress;    
            }
        }).start();

    }

    private void dodajVrijednostiUb(){

        for (int z=0;z<pitanja.size(); z++){
            b[z] = pitanja.get(z).getId_pitanja();
            numbers.add(b[z]);
        }

    }

    private void postaviPitanje(){

        while (!numbers.contains(c)){
            c = r.nextInt(22) + 1;
        }
        for (int i=0;i<pitanja.size(); i++){
            if (c==b[i]){
                d = pitanja.get(i).getTekst_pitanja();
                id_odgovor = pitanja.get(i).getOdgovor();
            }
        }
        t.setText(d);

    }

    private void postaviOdgovore(){

        odgovori = mDbHelper.getOdgovor(c);
        for (int i = 0; i<odgovori.size(); i++){

            final Button bt = new Button(this);
            bt.setId(odgovori.get(i).getId_odgovor());
            bt.setBackgroundResource(getResources().getIdentifier("buttoni", "drawable", getPackageName()));
            final String text = odgovori.get(i).getOdgovor();
            int l = odgovori.get(i).getId_odgovor();
            final String s = Integer.toString(l);
            bt.setText(text);
            buttonParams.setMargins(20, 5, 20, 5);
            bt.setLayoutParams(buttonParams);
            ln.addView(bt);
            bt.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View v) {

                    f = Integer.toString(id_odgovor);
                    if (f.equals(s)){
                        tocan = true;
                        bt.setTextColor(Color.GREEN);
                    } else {
                        tocan = false;
                        bt.setTextColor(Color.RED);
                        Button newbutton = (Button) findViewById(id_odgovor);
                        newbutton.setTextColor(Color.GREEN);
                    }
                    timer.cancel();
                        if (tocan){
                            rezultat++;
                            timer.cancel();
                            Toast.makeText(beze2.this, "Tocan odgovor, vas score je " + Integer.toString(rezultat), Toast.LENGTH_SHORT).show();
                            Intent i = new Intent(getApplicationContext(), beze2.class);
                            i.putExtra("REZULTAT", Integer.toString(rezultat));
                            i.putExtra("EXTRA_M", strin);
                            startActivity(i);
                        }
                        else {
                            Toast.makeText(beze2.this, "Netocan odgovor, vas konacni score je " + Integer.toString(rezultat), Toast.LENGTH_SHORT).show();
                            try {
                                Thread.sleep(2000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            } finally{
                                upisiDatoteku("Guest", rezultat);
                                Intent i = new Intent(beze2.this, MainActivity.class);
                                startActivity(i);
                            }
                        }
                }

            });

        }
        timer.start();

    }

    private void upisiDatoteku(String s, int rez){

        try {
            fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
            String newOne = s + "\t" + Integer.toString(rez);
            fos.write(newOne.getBytes());
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public class myCountDownTimer extends CountDownTimer{

        public myCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {
            text.setText("Time's up");
            myHandler.post(new Runnable(){

                @Override
                public void run() {

                    try {
                        Thread.sleep(3000);
                        Intent i = new Intent(beze2.this, beze.class);
                        startActivity(i);
                        Toast.makeText(beze2.this, "Isteklo vam je vrijeme, Vas score je " + Integer.toString(rezultat), Toast.LENGTH_SHORT).show();
                        upisiDatoteku("Guest",rezultat);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }

            });

        }

        @Override
        public void onTick(long millisUntilFinished) {
            int i = (int) millisUntilFinished / 1000;
            text.setText(Integer.toString(i));
        }

    }

0 个答案:

没有答案