暂停活动并按下屏幕锁定时保持手电筒的代码

时间:2017-05-24 07:28:37

标签: android flashlight screen-lock

我正在实施手电筒,我已经调用了所有功能i,e onDestroy() onBackpressed()以保持我的闪光灯亮起。但我有错误,如果我按下后,屏幕被锁定后,手电筒几秒钟后关闭。请帮我解释一下代码。

public class MainActivity extends AppCompatActivity {
    ImageButton btnSwitch;
    Camera camera;
    Camera.Parameters parameters;
    boolean isflash = false;
    boolean ison = false;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        btnSwitch = (ImageButton) findViewById(R.id.btnSwitch);
        if(getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH));
        {
            try {
                camera = Camera.open();
                parameters = camera.getParameters();
                isflash = true;
            }catch (Exception e ){
                Log.e(getString(R.string.app_name), "failed to open Camera");
                e.printStackTrace();
            }
        }

        btnSwitch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isflash)
                {
                    if (!ison)
                    {
                        btnSwitch.setImageResource(R.drawable.off);
                        parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                        camera.setParameters(parameters);
                        camera.startPreview();
                        ison = true;
                    }
                    else {
                        btnSwitch.setImageResource(R.drawable.btn_switch_on);
                        parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                        camera.setParameters(parameters);
                        camera.stopPreview();
                        ison = false;
                    }
            }
            else
                {
                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    builder.setTitle("Error.....");
                    builder.setMessage("FlashLight is not available on this device....");
                    builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            finish();
                        }
                    });
                    AlertDialog alertDialog = builder.create();
                    alertDialog.show();

                    }

                }
        });

    }
    @Override
    protected void onStop() {
        super.onStop();
        if(!ison){
            if (camera!=null)
            {
                camera.release();
                camera = null;
            }
        }
    }

    @Override
    public void onBackPressed() {
       super.onBackPressed();
        if(!ison){
            if (camera!=null)
            {
                camera.release();
                camera = null;
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if(!ison){
            if (camera!=null)
            {
                camera.release();
                camera = null;
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(!ison) {
            if (camera != null) {
                camera.release();
                camera = null;
            }
        }
    }
}

0 个答案:

没有答案