0

我有一个 Chronometer,但是当我改变我的视图(使用 ActionBar)时它会停止。

我不是很了解如何使用 AsyncTask,也没有找到教程。

有什么容易解决我的问题吗?我真的很想举一个Chronometer的例子......

非常感谢!

到目前为止的代码:

public class BFragment extends SherlockFragment {

private Button start;
private View v;
private Chronometer chrono;
private Button pause;
private Button reset;
private long lastPause;
private Button resume;
private Boolean richtig;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    v = inflater.inflate(R.layout.activity_bfragment, container, false);
    start = (Button) v.findViewById(R.id.start); 
    chrono = (Chronometer) v.findViewById(R.id.chronometer1);
    pause = (Button) v.findViewById(R.id.pause);
    reset = (Button) v.findViewById(R.id.reset);
    resume = (Button) v.findViewById(R.id.resume);

    richtig = true;

    new chronom().execute();



    return v;

}
private class chronom extends AsyncTask<URL, Integer, Long> {
    protected Long doInBackground(URL... urls) {
        start.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                chrono.setBase(SystemClock.elapsedRealtime());
                chrono.start();

                System.out.println(SystemClock.elapsedRealtime());
                richtig = false;
            }
        });

        pause.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                lastPause = SystemClock.elapsedRealtime();
                chrono.stop();
                richtig = true;

            }
        });

        resume.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                if (richtig) {
                    chrono.setBase(chrono.getBase() + SystemClock.elapsedRealtime() - lastPause);
                    chrono.start();
                } else {

                }
            }
        });

        reset.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                chrono.setBase(SystemClock.elapsedRealtime());
                System.out.println(chrono.getBase());
                richtig = false;
            }
        });

        return null;
    }

    protected void onProgressUpdate(Integer... progress) {

    }

    protected void onPostExecute(Long result) {

    }
}
}

我刚刚复制了代码......(Chrono 工作,但不在后台)

4

0 回答 0