FlashLight不适用于Android

时间:2017-03-25 10:13:48

标签: java android

我尝试通过按钮激活手电筒,起初它刚刚崩溃,现在它没有崩溃,但没有发生任何事情,我尝试这个应用程序我的LG V10,没有什么好处,闪光剂量工作,我不知道该怎么办。 感谢。

  //pulse check variebls
private CountDownTimer countDownTimer;
private Button pulse,start,cal;
private TextView time,tvah;
private EditText howmuch;
//eyes check variebls
private Camera camera;
private boolean isFlashOn;
private boolean hasFlash;
Parameters params;
private Button on;
private Button flash;
//breath check
private Button breath;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

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

    //pulse test variebles
    howmuch = (EditText) findViewById(R.id.Howmuch);
    cal = (Button) findViewById(R.id.cal);
    pulse = (Button) findViewById(R.id.pulse);
    start = (Button) findViewById(R.id.start);
    time = (TextView) findViewById(R.id.time);
    tvah = (TextView) findViewById(R.id.tvah);


    //pulse check start
    pulse.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            tvah.setVisibility(View.INVISIBLE);
            time.setVisibility(View.INVISIBLE);
            howmuch.setVisibility(View.INVISIBLE);
            cal.setVisibility(View.INVISIBLE);
            start.setVisibility(View.VISIBLE);
            time.setVisibility(View.VISIBLE);
            on.setVisibility(View.INVISIBLE);
            time.setText("00:15:00");
            //start button
            start.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (start.getText().equals("התחל!")) {
                        //start
                        start.setBackgroundColor(Color.RED);
                        start.setText("עצור!");
                        countDownTimer = new CountDownTimer(15 * 1000, 100) {
                            @Override
                            public void onTick(long millisUntilFinished) {
                                time.setText("00:" + millisUntilFinished / 1000 + ":" + millisUntilFinished % (int) Math.pow(10, (int) Math.log10(millisUntilFinished))/10);
                            }

                            @Override
                            public void onFinish() {
                                time.setText("נגמר הזמן!");
                                start.setBackgroundColor(Color.GREEN);
                                start.setText("התחל!");
                                howmuch.setVisibility(View.VISIBLE);
                                cal.setVisibility(View.VISIBLE);
                                //cal start
                                cal.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        int num = Integer.parseInt(howmuch.getText().toString());
                                        howmuch.setHint(num + " * 4 = " + num * 4);
                                        howmuch.setText(null);
                                        if (num * 4 >= 60 && num * 4 <= 100) {
                                            tvah.setText("דופק תקין!");
                                            tvah.setTextColor(Color.GREEN);
                                        } else if (num * 4 > 100) {
                                            tvah.setText("דופק לא תקין! גבוה.");
                                            tvah.setTextColor(Color.RED);
                                        } else {
                                            tvah.setText("דופק לא תקין! נמוך.");
                                            tvah.setTextColor(Color.RED);
                                        }
                                        tvah.setVisibility(View.VISIBLE);

                                    }
                                });
                                //cal ends
                            }
                        };
                        countDownTimer.start();
                    } else {
                        //stop
                        start.setBackgroundColor(Color.GREEN);
                        start.setText("התחל!");
                        if (countDownTimer != null) {
                            countDownTimer.cancel();
                            countDownTimer = null;
                        }
                    }


                }
            });
            //stop start button

        }
    });
    //stop pulse check

    //eyes check start

    //breath start
    breath = (Button) findViewById(R.id.breath);
    breath.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            tvah.setVisibility(View.INVISIBLE);
            time.setVisibility(View.INVISIBLE);
            on.setVisibility(View.INVISIBLE);
            howmuch.setVisibility(View.INVISIBLE);
            cal.setVisibility(View.INVISIBLE);
            start.setVisibility(View.VISIBLE);
            time.setVisibility(View.VISIBLE);
            time.setText("00:30:00");
            //start button
            start.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (start.getText().equals("התחל!")) {
                        //start
                        start.setBackgroundColor(Color.RED);
                        start.setText("עצור!");
                        countDownTimer = new CountDownTimer(30 * 1000, 100) {
                            @Override
                            public void onTick(long millisUntilFinished) {
                                time.setText("00:" + millisUntilFinished / 1000 + ":" + millisUntilFinished % (int) Math.pow(10, (int) Math.log10(millisUntilFinished))/10);
                            }

                            @Override
                            public void onFinish() {
                                time.setText("נגמר הזמן!");
                                start.setBackgroundColor(Color.GREEN);
                                start.setText("התחל!");
                                howmuch.setVisibility(View.VISIBLE);
                                cal.setVisibility(View.VISIBLE);
                                //cal start
                                cal.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        int num = Integer.parseInt(howmuch.getText().toString());
                                        howmuch.setHint(num + " * 2 = " + num * 2);
                                        howmuch.setText(null);
                                        if (num * 2 >= 12 && num * 2 <= 20) {
                                            tvah.setText("נשימות תקינות!");
                                            tvah.setTextColor(Color.GREEN);
                                        } else if (num * 2 > 20) {
                                            tvah.setText("נשימות לא תקינות! יותר מדי נשימות.");
                                            tvah.setTextColor(Color.RED);
                                        } else {
                                            tvah.setText("נשימות לא תקינות! פחות מדי נשימות.");
                                            tvah.setTextColor(Color.RED);
                                        }
                                        tvah.setVisibility(View.VISIBLE);

                                    }
                                });
                                //cal ends
                            }
                        };
                        countDownTimer.start();
                    } else {
                        //stop
                        start.setBackgroundColor(Color.GREEN);
                        start.setText("התחל!");
                        if (countDownTimer != null) {
                            countDownTimer.cancel();
                            countDownTimer = null;
                        }
                    }


                }
            });
            //stop start button

        }
    });
    //breath ends


    flash = (Button) findViewById(R.id.flash);
    on = (Button) findViewById(R.id.on);




    flash.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            start.setVisibility(View.INVISIBLE);
            cal.setVisibility(View.INVISIBLE);
            tvah.setVisibility(View.INVISIBLE);
            time.setVisibility(View.INVISIBLE);
            howmuch.setVisibility(View.INVISIBLE);



            if (!hasFlash) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("שגיאה!");
                builder.setMessage("במכשיר זה , לא קיימת אפשרות להדליק פנס!");
                builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                AlertDialog alertDialog = builder.create();
                alertDialog.show();
                on.setVisibility(View.INVISIBLE);
            }
            else {
                getCamera();
                on.setBackgroundColor(Color.GREEN);
                on.setVisibility(View.VISIBLE);
            }
        }
    });

    //flash start
    on.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (isFlashOn) {
                turnOffFlash();
                on.setBackgroundColor(Color.GREEN);
                on.setText("הדלק פנס!");
            }
            else {
                turnOnFlash();
                on.setBackgroundColor(Color.RED);
                on.setText("עצור פנס!");
            }
        }
    });
    //flash finished




}


private void getCamera() {

    if (camera == null) {
        try {
            camera = Camera.open();
            params = camera.getParameters();
        }catch (Exception e) {

        }
    }

}


private void turnOnFlash() {

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

        params = camera.getParameters();
        params.setFlashMode(Parameters.FLASH_MODE_AUTO);
        camera.setParameters(params);
        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);
        isFlashOn = false;
    }
}
@Override
protected void onDestroy() {
    super.onDestroy();
}

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

    // on pause turn off the flash
    turnOffFlash();
}

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

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

    // on resume turn on the flash
    if(hasFlash)
        turnOnFlash();
}

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

    // on starting the app get the camera params
    getCamera();
}

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

    // on stop release the camera
    if (camera != null) {
        camera.release();
        camera = null;
    }
}

0 个答案:

没有答案