在新线程中每秒写入Android日志

3

我希望能够在新线程中将SeekBar的值写入日志。我想按下按钮并开始写入日志,然后改变SeekBar的值并将其写入日志。

public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener, View.OnTouchListener {

    private SeekBar seekBar;
    private Button button;
    private TextView textView;
    private EditText editText;
    private Thread thread;
    private int value = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        seekBar = (SeekBar) findViewById(R.id.seekBar);
        button = (Button) findViewById(R.id.button);
        textView = (TextView) findViewById(R.id.textView);
        editText = (EditText) findViewById(R.id.editText);
        seekBar.setOnSeekBarChangeListener(this);
        button.setOnTouchListener(this);
        print();
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        value = progress;
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                print();
                Toast.makeText(MainActivity.this, "ACTION_DOWN", Toast.LENGTH_SHORT).show();
                return true; // if you want to handle the touch event
            case MotionEvent.ACTION_UP:
                thread.stop();
                Toast.makeText(MainActivity.this, "ACTION_UP", Toast.LENGTH_SHORT).show();
                return true; // if you want to handle the touch event
        }
        return false;
    }

    public void print() {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                Log.d("MyLog", String.valueOf(value));
            }
        };
        thread = new Thread(runnable);
        thread.start();
    }
}

但我的日志只写一次。我该如何每秒写入日志?

2个回答

3
你可以像这样做:

您可以按照以下步骤进行操作:

Handler handler = new Handler();
boolean stop = false;

Runnable runnable = new Runnable() {
    @Override
    public void run() {
        Log.d("MyLog", String.valueOf(value));

        if (!stop) {
            handler.postDelayed(this, 1000);
        }
    }
};

// start logging by calling this method
public void print() {
    handler.post(runnable);
}

// stop logging by calling this method
public void printStop() {
    stop = true;
}

0

对我有效:

new Thread(new Runnable() {
            @Override
            public void run() {
                while(true) {
                    //do whatever you want
                    try {
                        Thread.sleep(1000); //sleep time in milliseconds
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接