无法停止线程onBackPressed

时间:2014-10-27 12:09:40

标签: java android multithreading

我有一个名为runner的线程。我想停止这个线程onBackPressed但这不会发生。这是代码:

public class StrobeLightConfig extends Activity {

    Camera cam;
    StrobeRunner runner;
    Thread bw;
    CheckBox strobe;
    private boolean isFlashOn;
    private boolean hasFlash;
    private Parameters params;
    private MediaPlayer mp;
    VerticalSeekBar skbar, skbaroff;
    ToggleButton togglebutton;

    public final Handler mHandler = new Handler();

    public final Runnable mShowToastRunnable = new Runnable() {
        public void run() {
            showMessage();
        }
    };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        skbar = (VerticalSeekBar) findViewById(R.id.SeekBar01);
        strobe = (CheckBox) findViewById(R.id.checkBox1);
        togglebutton = (ToggleButton) findViewById(R.id.ToggleButton01);
        skbaroff = (VerticalSeekBar) findViewById(R.id.SeekBar02);

        hasFlash = getApplicationContext().getPackageManager()
                .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

        turnOnFlash();

        strobe.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {

                if (buttonView.isChecked()) {
                    cam.release();
                    Toast.makeText(getApplicationContext(), "Released 1", 0)
                            .show();
                } else {
                    cam.release();
                    Toast.makeText(getApplicationContext(), "Released 2", 0)
                            .show();
                }

            }
        });

        runner = StrobeRunner.getInstance();
        runner.controller = this;

        if (runner.isRunning) {

        } else {
            try {

                if (cam == null) {
                cam = Camera.open();
                }


            } catch (RuntimeException ex) {
                togglebutton.setEnabled(false);
                TextView t = (TextView) findViewById(R.id.TextViewHW2);
                t.setText(R.string.nocamera);
                Toast.makeText(getApplicationContext(),
                        "Error connecting to camera flash.", Toast.LENGTH_LONG)
                        .show();
                return;
            }
        }

        togglebutton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // Perform action on clicks
                if (strobe.isChecked()) {
                    if (togglebutton.isChecked()) {
                        turnOnStrobe();
                    } else {
                        turnOffStrobe();
                    }
                } else {

                    if (isFlashOn) {
                        // turn off flash
                        turnOffFlash();
                    } else {
  ----------------- runner.requeststop working here---------------------
                        turnOnFlash();
                    }

                }

            }
        });


        skbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                runner.delay = progress;

            }
        });


        skbaroff.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                runner.delayoff = progress;

            }
        });



    }

    private void getCamera() {
        if (cam == null) {
            try {
                cam = Camera.open();
                params = cam.getParameters();
            } catch (RuntimeException e) {
                Log.e("Camera Error. Failed to Open. Error: ", e.getMessage());
            }
        }
    }

    /*
     * Turning On flash
     */
    private void turnOnFlash() {
        if (!isFlashOn) {
            togglebutton.setChecked(true);
            cam = Camera.open();
            params = cam.getParameters();
            params.setFlashMode(Parameters.FLASH_MODE_TORCH);
            cam.setParameters(params);
            cam.startPreview();
            isFlashOn = true;
            strobe.setEnabled(false);
        }

    }

    private void turnOffFlash() {
        if (isFlashOn) {
            togglebutton.setChecked(false);
            params = cam.getParameters();
            params.setFlashMode(Parameters.FLASH_MODE_OFF);
            cam.setParameters(params);
            cam.stopPreview();
            cam.release();
            isFlashOn = false;
            strobe.setEnabled(true);

        }
    }

    private void turnOnStrobe() {
        if (!isFlashOn) {
            strobe.setEnabled(false);
            bw = new Thread(runner);
            bw.start();
        }

    }

    private void turnOffStrobe() {
        if (isFlashOn) {

                runner.requestStop = true;
                strobe.setEnabled(true);

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onPause() {
        super.onPause();

    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    public void showMessage() {
        String err = runner.errorMessage;
        runner.errorMessage = "";
        if (!err.equals("")) {
            Context context = getApplicationContext();
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(context, err, duration);
            toast.show();
        }

        ToggleButton togglebutton = (ToggleButton) findViewById(R.id.ToggleButton01);
        togglebutton.setChecked(false);
    }

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();

        if (strobe.isChecked()) {
            turnOffStrobe();
        } else {

            if (isFlashOn) {
                ----------------- runner.requeststop needs to work here---------------------    
                turnOffFlash();
                finish();
                moveTaskToBack(true);
            }
        }
    }
}

但是,如果我写

runner.requestStop = true;
strobe.setEnabled(true);

在oncreate(而不是onBackPressed)的TurnoffStrobe所在地,它正在工作。如果 runner.requeststop 在onCreate外工作,我该怎么做?

1 个答案:

答案 0 :(得分:0)

尝试 onBackpress()中的 your_thread.isInterrupted();