Flying after flash



  • I'd like to make it burn in 10 seconds.

    Help me with the sugar.

    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.DialogInterface;
    import android.content.pm.PackageManager;
    import android.hardware.Camera;
    import android.hardware.Camera.Parameters;
    

    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.TextView;

    public class MainActivity extends Activity {
    TextView textView;
    private Camera camera;
    private boolean isFlashOn;
    private boolean hasFlash;
    EditText editText;
    long sek=0;
    Parameters params;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView=(TextView)findViewById(R.id.textView);
    editText=(EditText)findViewById(R.id.editText);

        //Проверяем поддержку работы с фонариком на устройстве:
        hasFlash = getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
        if (!hasFlash) {
            //Если вспышка не поддерживается, показываем
            //диалоговое окно с ошибкой и закрываем приложение:
            AlertDialog alert = new AlertDialog.Builder(MainActivity.this).create();
            alert.setTitle("Ошибка");
            alert.setMessage("Ваше устройство не поддерживает работу со вспышкой!");
            alert.setButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    //Закрываем приложение:
                    finish();
                }
            });
            alert.show();
            return;
        }
        getCamera();
    
    }
    //Получаем параметры камеры:
    private void getCamera() {
        if (camera == null) {
            try {
                camera = Camera.open();
                params = camera.getParameters();
    
            } catch (RuntimeException e) {
                Log.e("Ошибка, невозможно запустить: ", e.getMessage());
            }
        }
    }
    //Включаем вспышку
    private void turnOnFlash() {
        if (!isFlashOn) {
            if (camera == null || params == null) {
                return;
            }
            params = camera.getParameters();
            params.setFlashMode(Parameters.FLASH_MODE_TORCH);
            camera.setParameters(params);
            camera.startPreview();
            isFlashOn = true;
    
        }
    
    }
    //Выключаем фонарик
    private void turnOffFlash() {
        if (isFlashOn) {
            if (camera == null || params == null) {
                return;
            }
            params = camera.getParameters();
            params.setFlashMode(Parameters.FLASH_MODE_OFF);
            camera.setParameters(params);
            camera.stopPreview();
            isFlashOn = false;
        }
    }
    
    
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
    
    @Override
    protected void onPause() {
        super.onPause();
    
        //Временно выключаем фонарик:
        turnOffFlash();
    }
    
    @Override
    protected void onRestart() {
        super.onRestart();
    }
    
    
    
    @Override
    protected void onStart() {
        super.onStart();
    
        //Получаем для приложения параметры камеры:
        getCamera();
    }
    
    @Override
    protected void onStop() {
        super.onStop();
    
        //Закрываем работу камеры:
        if (camera != null) {
            camera.release();
            camera = null;
        }
    }
    
    public void vikl(View view) {
        turnOffFlash();
        textView.setText("выкл)");
    
    }
    
    public void vlk(View view) {
    
        Thread thread = new Thread(new Runnable() {
    
            @Override
            public void run() {
                turnOnFlash();
                try {
                    textView.setText("Да будет свет");
                    Thread.sleep(10000);
                    turnOffFlash();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();
    }
    

    }

    Mistake:

    11-24 15:11:10.854 2199-3922/com.example.maxim.fonarik E/AndroidRuntime: FATAL EXCEPTION: Thread-121
    11-24 15:11:10.854 2199-3922/com.example.maxim.fonarik E/AndroidRuntime: Process: com.example.maxim.fonarik, PID: 2199
    11-24 15:11:10.854 2199-3922/com.example.maxim.fonarik E/AndroidRuntime: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread created that a view hierarchy can touch its views.
    11-24 15:11:10.854 2199-3922/com.example.maxim.fonarik E/AndroidRuntime: at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6556)
    11-24 15:11:10.854 2199-3922/com.example.maxim.fonarik E/AndroidRuntime: at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:907)
    11-24 15:11:10.854 2199-3922/com.example.maxim.fonarik E/AndroidRuntime: at android.view.View.requestLayout(View.java:18722)
    11-24 15:11:10.854 2199-3922/com.example.maxim.fonarik E/AndroidRuntime: at android.view.View.requestLayout(View.java:18722)
    11-24 15:11:10.854 2199-3922/com.example.maxim.fonarik E/AndroidRuntime: at android.view.View.requestLayout(View.java:18722)
    11-24 15:11:10.854 2199-3922/com.example.maxim.fonarik E/AndroidRuntime: at android.view.View.requestLayout(View.java:18722)
    11-24 15:11:10.854 2199-3922/com.example.maxim.fonarik E/AndroidRuntime: at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:360)
    11-24 15:11:10.854 2199-3922/com.example.maxim.fonarik E/AndroidRuntime: at android.view.View.requestLayout(View.java:18722)
    11-24 15:11:10.854 2199-3922/com.example.maxim.fonarik E/AndroidRuntime: at android.widget.TextView.checkForRelayout(TextView.java:7172)
    11-24 15:11:10.854 2199-3922/com.example.maxim.fonarik E/AndroidRuntime: at android.widget.TextView.setText(TextView.java:4342)
    11-24 15:11:10.854 2199-3922/com.example.maxim.fonarik E/AndroidRuntime: at android.widget.TextView.setText(TextView.java:4199)
    11-24 15:11:10.854 2199-3922/com.example.maxim.fonarik E/AndroidRuntime: at android.widget.TextView.setText(TextView.java:4174)
    11-24 15:11:10.854 2199-3922/com.example.maxim.fonarik E/AndroidRuntime: at com.example.maxim.fonarik.MainActivity$2.(runMainActivity.java:147)
    11-24 15:11:10.854 2199-3922/com.example.maxim.fonarik E/AndroidRuntime: at java.lang.Thread.run(Thread.java:818)
    11-24 15:11.114 2199-2217/com.example.maxim.fonarik E/Surface: getSlotFromBufferLocked: unknown buffer: 0xabdfa170
    11-24 15:11:16.743 2199-2199/com.example.maxim.fonarik I/Choreographer: Skipped 341 frames! The application may be doing too much work on its main thread.



  • Exception says: Only the original thread that created a view hierarchy can touch its views♪ So you're trying. Change some elements of the interface from the wrong track.♪ Android can only change the interface so-called UI Thread (sighs)User Interface Thread)

    In your case, textView.setText("Да будет свет"); - where there's a mistake.

    To do this in UI, use it. Method runOnUiThreadContext(sighs)Activity - Yeah. Context) :

    runOnUiThread(new Runnable(){
    
    @Override
    public void run(){
        textView.setText("Да будет свет");
    }
    

    });

    Although it's gonna work, it's a design, as you can see, incorrect and cumbersome.
    To read http://developer.android.com/intl/ru/reference/android/os/Handler.html to fulfil the code within a time frame, or http://developer.android.com/intl/ru/reference/android/os/AsyncTask.html for routinely doing something in a separate flow.


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2