0
我是android新手。我做了一个简单的火炬灯应用程序。一切都很好,但闪烁的部分正在造成问题。如果我按下“眨眼”按钮,手电筒开始闪烁,但是当我再次按下按钮时,闪烁不会停止,而是闪烁加起来。例如,如果我按下按钮两次,则该功能执行两次。如果它为了那么它必须在第二次按下时停止。任何人都有解决方案。请帮忙。按下按钮时不停止的方法
这里是MainActivity.java
package epicerastudios.torch;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
public class MainActivity extends AppCompatActivity {
private CameraManager mCameraManager;
private String mCameraId;
private ImageButton mTorchOnOffButton;
private ImageButton mBlinkOnOffButton;
private Boolean isTorchOn;
private Boolean isBlinkOn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("FlashLightActivity", "onCreate()");
setContentView(R.layout.activity_main);
mTorchOnOffButton = (ImageButton) findViewById(R.id.Switch);
mBlinkOnOffButton = (ImageButton) findViewById(R.id.Blink) ;
isTorchOn = false;
isBlinkOn = false;
Boolean isFlashAvailable = getApplicationContext().getPackageManager()
.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if (!isFlashAvailable) {
AlertDialog alert = new AlertDialog.Builder(MainActivity.this)
.create();
alert.setTitle("Error !!");
alert.setMessage("Your device doesn't support flash light!");
alert.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// closing the application
finish();
System.exit(0);
}
});
alert.show();
return;
}
mCameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
mCameraId = mCameraManager.getCameraIdList()[0];
} catch (CameraAccessException e) {
e.printStackTrace();
}
mTorchOnOffButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
if (isTorchOn) {
turnOffFlashLight();
isTorchOn = false;
} else {
turnOnFlashLight();
isTorchOn = true;
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
mBlinkOnOffButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isBlinkOn) {
turnOffBlink();
isBlinkOn = false;
} else {
turnOnBlink();
isBlinkOn = true;
}
}
});
}
public void turnOnFlashLight() {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mCameraManager.setTorchMode(mCameraId, true);
mTorchOnOffButton.setImageResource(R.drawable.btn_on);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void turnOffFlashLight() {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mCameraManager.setTorchMode(mCameraId, false);
mTorchOnOffButton.setImageResource(R.drawable.btn_off);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void turnOnBlink() {
mBlinkOnOffButton.setImageResource(R.drawable.blink_on);
String myString = "0101010101";
long blinkDelay = 300; //Delay in ms
for (int i = 0; i < myString.length(); i++) {
if (myString.charAt(i) == '0') {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mCameraManager.setTorchMode(mCameraId, true);
}
} catch (Exception e) {
e.printStackTrace();
}
}
else {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mCameraManager.setTorchMode(mCameraId, false);
}
}
catch (Exception e){
e.printStackTrace();
}
}
try {
Thread.sleep(blinkDelay);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void turnOffBlink() {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mCameraManager.setTorchMode(mCameraId, false);
mBlinkOnOffButton.setImageResource(R.drawable.blink_off);
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onStop() {
super.onStop();
if(isTorchOn || isBlinkOn){
turnOffFlashLight();
turnOffBlink();
}
}
@Override
protected void onPause() {
super.onPause();
if(isTorchOn || isBlinkOn){
turnOffFlashLight();
turnOffBlink();
}
}
@Override
protected void onResume() {
super.onResume();
if(isTorchOn || isBlinkOn){
turnOnFlashLight();
turnOnBlink();
}
}
}
这里是我的activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="epicerastudios.torch.MainActivity"
android:background="#fff">
<ImageButton
android:id="@+id/Switch"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:background="#fff"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/btn_off" />
<ImageButton
android:id="@+id/Blink"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@id/Switch"
android:layout_alignLeft="@id/Switch"
android:background="#fff"
android:src="@drawable/blink_off" />
</RelativeLayout>
现在它不是闪烁所有.. – Sheri
我已经编辑我的答案。应该在调用启动动作的方法之前调用isBlinkOn。 – Kristopher
感谢您的回答......它开始闪烁,但问题保持不变 – Sheri