title: Android App does not work on run time taking some time to work, how to solve this issue
body: <p>I am new in android development and learning android apps development. I have created a very basic and simple Flashlight for android device. I am facing the issue when i run the app it takes some time to run like if i press turn on flash light it will take some time (half sec or less but it take some time), i didn't use wait() method in my app. How to run it really fast like user click on it flash turn on or turn off?</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
  private ImageButton imageButton;
  private Camera camera;
  private boolean isFlashOn;
  private boolean hasFlash;
  private Camera.Parameters params;
  private MediaPlayer mp;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      imageButton = (ImageButton) findViewById(R.id.switch_btn);
  
      //Check that Device has supports flash or not
  
      hasFlash = getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
  
      if (!hasFlash){
          //If device does not supports Flash
          AlertDialog alert = new AlertDialog.Builder(MainActivity.this).create();
          alert.setTitle("Error");
          alert.setMessage("Sorry, your current device does not support to Little Flashy! ops");
          alert.setButton("Ok", new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {
                  //Close application
                  finish();
              }
          });
          alert.show();
          return;
      }
  
  
      //Get the Camera
      getCamera();
      //Display button image
      toggleButtonImage();
  
      imageButton.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              if (isFlashOn) {
                  turnOffFlash();
              } else
  
              {
                  turnOnFlash();
              }
          }
      });
  }
  
  private void toggleButtonImage() {
      if (isFlashOn){
          imageButton.setImageResource(R.drawable.btn_switch_on);}
      else {imageButton.setImageResource(R.drawable.btn_switch_off);}
  
      }
  
  private void getCamera() {
      if (camera == null){
          try{
              camera = camera.open();
              params = camera.getParameters();
          }catch (RuntimeException e){
              Log.d("Camera Error.", e.getMessage());
          }
      }
  }
  
  /*
   * Turning On flash
   */
  private void turnOnFlash() {
      if (!isFlashOn) {
          if (camera == null || params == null) {
              return;
          }
          // play sound
          playSound();
  
          params = camera.getParameters();
          params.setFlashMode(Parameters.FLASH_MODE_TORCH);
          camera.setParameters(params);
          camera.startPreview();
          isFlashOn = true;
  
          // changing button/switch image
          toggleButtonImage();
      }
  
  }
  
  /*
   * Turning Off flash
   */
  private void turnOffFlash() {
      if (isFlashOn) {
          if (camera == null || params == null) {
              return;
          }
          // play sound
          playSound();
  
          params = camera.getParameters();
          params.setFlashMode(Parameters.FLASH_MODE_OFF);
          camera.setParameters(params);
          camera.stopPreview();
          isFlashOn = false;
  
          // changing button/switch image
          toggleButtonImage();
      }
  }
  
  private void playSound() {
      if (isFlashOn){
          mp = MediaPlayer.create(MainActivity.this, R.raw.light_switch_off);}
      else {
          mp= MediaPlayer.create(MainActivity.this, R.raw.light_switch_on);
      }
      mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
          @Override
          public void onCompletion(MediaPlayer mp) {
              mp.release();
          }
      });
      mp.start();
  }
  
  @Override
  protected void onDestroy() {
      super.onDestroy();
  }
  
  @Override
  protected void onPause() {
      super.onPause();
  
      //turn off flash when on Pause called
      turnOffFlash();
  }
  
  @Override
  protected void onRestart() {
      super.onRestart();
  }
  
  @Override
  protected void onResume() {
      super.onResume();
  
      if (hasFlash) turnOnFlash();
  }
  
  @Override
  protected void onStart() {
      super.onStart();
      getCamera();
  }
  
  @Override
  protected void onStop() {
      super.onStop();
  
      if (camera != null){
          camera.release();
          camera = null;
      }
  }
  }
  </code></pre>