【问题标题】:My android application is not working with respect to activity life cycle我的 android 应用程序在活动生命周期方面不起作用
【发布时间】:2017-11-24 03:15:02
【问题描述】:

我正在制作带有处理活动生命周期的手电筒应用程序。应用程序运行良好,但是当我调用 onStop(); 时出现问题;当手电筒打开时,当我从 onStop(); 返回时,应用程序应该打开手电筒,但它没有。 我已经尝试了所有方法,但 flashOn();没有启用手电筒。我从调试中检查过,如果从 onStop() 返回后手电筒打开,应用程序什么也不做;

public class MainActivity extends AppCompatActivity {

private ImageButton imagebtn;
ImageView img;

private Camera camera;
private boolean isFlashOn;
private boolean hasFlash = false;
private Camera.Parameters params;

private boolean flag= false;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    imagebtn = (ImageButton) findViewById(R.id.button);
    img = findViewById(R.id.torchimage);

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

    if (!hasFlash) {
        // If device doesn't support flash
        // Show alert message and close the application
        AlertDialog alert = new AlertDialog.Builder(MainActivity.this)
                .create();
        alert.setTitle("Error");
        alert.setMessage("Sorry, your device doesn't support flash light!");
        alert.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

                finish();//Close application
            }
        });
        alert.show();
    }


    imagebtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (isFlashOn) {

                flashOff();
            } else {

                flashOn();
            }
        }
    });
}


protected void checkCamera() {
    if (camera == null) {
        try {
            camera = Camera.open();
            params = camera.getParameters();
        } catch (RuntimeException e) {
            Toast.makeText(getApplicationContext(), "Camera not found", Toast.LENGTH_SHORT).show();
        }
    }
}


**protected void flashOn() {
    if (!isFlashOn) {


       {
                if (camera == null || params == null) {
                    return;
                }

            /*if (flag==true) {
flag=false;
                params = camera.getParameters();
                params.setFlashMode(Parameters.FLASH_MODE_TORCH);
                camera.setParameters(params);
                camera.startPreview();
            */}

        params = camera.getParameters();
        params.setFlashMode(Parameters.FLASH_MODE_TORCH);
        camera.setParameters(params);
        camera.startPreview();
        isFlashOn = true;
        toggleImages();
        btnSound();
    }
}**

protected void flashOff() {
    if (isFlashOn)
        {
        if (camera == null || params == null) {
            return;
        }

        params = camera.getParameters();
        params.setFlashMode(Parameters.FLASH_MODE_OFF);
        camera.setParameters(params);
        camera.stopPreview();

        isFlashOn = false;
            toggleImages();
        btnSound();

    }
}

protected void btnSound() {
    final MediaPlayer mp = MediaPlayer.create(this, R.raw.button_sound);
    mp.start();
}

public void toggleImages() {
    if (isFlashOn) {
        imagebtn.setImageResource(R.drawable.button_on);
        img.setImageResource(R.drawable.torch_on);
    } else {
        imagebtn.setImageResource(R.drawable.button_off);
        img.setImageResource(R.drawable.torch_off);
    }
}

@Override


 protected void onDestroy() {
      //  Toast.makeText(this,"OnDestroy",Toast.LENGTH_SHORT).show();
        super.onDestroy();
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (isFlashOn)
            flashOn();
        else
            flashOff();

    }

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

            if (isFlashOn==true)
                flashOn();
            else
                flashOff();

    }

@Override
protected void onResume() {
    super.onResume();
        if (isFlashOn == true)
            flashOn();
        else
            flashOff();
}

@Override
protected void onStart() {
    super.onStart();
  // Toast.makeText(this,"OnStart",Toast.LENGTH_SHORT).show();

//  if (hasFlash)
      checkCamera();


}


@Override
protected void onStop() {
//    Toast.makeText(this, "OnStop", Toast.LENGTH_SHORT).show();
    super.onStop();
    if (camera != null) {
        camera.release();
       camera = null;
       flag= true;

    }

}

【问题讨论】:

  • 这种情况下它到达了 onstop 但它没有在 flash 上?
  • @sasikumar 在手电筒打开时调用 onStop 时。但是在重新启动应用程序时,闪光灯不在相机上

标签: java android activity-lifecycle android-studio-3.0 flashlight


【解决方案1】:

请再次查看Android生命周期https://developer.android.com/guide/components/activities/activity-lifecycle.html

它继续 onStop -> onRestart -> onStart -> onResume

你有很多废话散布在整个地方,使得应该很容易看到的东西变得相当困难。

所以...闪光灯已打开,即 isFlashOn = true;

从 if 中删除 boolean == true 。只要 (boolean) 有效并且更好。

onStop... 相机 = null;标志=真;

但 isFlashOn 仍然是真的

返回活动...

onRestart... if (isFlashOn==true) flashOn();

flashOn() { if (!isFlashOn) {

onStart... checkCamera() { if (camera == null) {

onResume... if (isFlashOn == true)

在 onStop 中设置 isFlashOn = false

还从恢复或重新启动中删除代码...它只是重复并经历了两次相同的事情。

希望这能教您如何更好地调试。从中学习。

【讨论】:

  • 我已经按照您的建议进行了更改。但问题是相同的@cmosBattery 现在开始它没有得到相机。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-13
  • 2011-12-18
  • 1970-01-01
  • 2011-06-02
相关资源
最近更新 更多