在应用程序中有一个图像按钮,可以打开和关闭手电筒,如果手电筒打开,那么你可以用搜索栏调整频闪频率,但是当我尝试在手机上运行应用程序时,那么图像按钮什么都不做。 下面是代码:
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraManager;
import android.os.Build;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;
import static android.content.DialogInterface.*;
public class Flashlight extends AppCompatActivity {
CameraManager cm;
private ImageButton flashlightButton;
private boolean flashlightOnOrOff;
private String mCameraId;
int freg;
Thread th;
Strobo sr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flashlight);
flashlightButton = (ImageButton) findViewById(R.id.flashOnOffButton);
flashlightOnOrOff = false;
//Error if device does not have flashlight
boolean hasFlash = this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if (hasFlash == false)
{
final AlertDialog dialo = new AlertDialog.Builder(Flashlight.this).create();
dialo.setTitle("Error");
dialo.setMessage("Sorry your device does not have flashlight!");
dialo.setButton(BUTTON_POSITIVE, "OK", new OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialo.cancel();
}
});
dialo.show();
}
cm = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
mCameraId = cm.getCameraIdList()[0];
} catch (CameraAccessException ec) {
ec.printStackTrace();
}
//Seekbar
SeekBar skbar = (SeekBar) findViewById(R.id.seekBar);
skbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
freg = i;
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
public void flashButtonClicked(View view) {
try {
if (flashlightOnOrOff) {
flashlightOnOrOff = false;
if (th != null) {
sr.stopRunning = true;
th = null;
return;
} else {
turnOffFlashlight();
}
} else {
flashlightOnOrOff = true;
if (freg != 0) {
sr = new Strobo();
sr.freg = freg;
th = new Thread(sr);
th.start();
return;
} else {
turnOnFlashlight();
}
}
} catch (Exception ec) {
ec.printStackTrace();
}
}
public class Strobo implements Runnable {
int freg;
boolean stopRunning = false;
@Override
public void run() {
try {
while (!stopRunning) {
turnOnFlashlight();
Thread.sleep(100 - freg);
turnOffFlashlight();
Thread.sleep(freg);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void turnOffFlashlight() {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
cm.setTorchMode(mCameraId, false);
flashlightButton.setImageResource(R.drawable.offbutton2);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void turnOnFlashlight() {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
cm.setTorchMode(mCameraId, true);
flashlightButton.setImageResource(R.drawable.onbutton2);
}
} catch (Exception e) {
e.printStackTrace();
}
}
XML:
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/offbutton2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="@+id/flashOnOffButton"
android:background="#000"
android:onClick="flashButtonClicked"
android:clickable="true"
android:scaleType="fitCenter"
android:adjustViewBounds="false" />
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:id="@+id/seekBar"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_marginTop="25dp"
style="@style/Widget.AppCompat.SeekBar"
android:thumbTint="@color/Greeny"
android:layout_below="@+id/flashOnOffButton"
android:layout_alignParentStart="true" />
答案 0 :(得分:0)
你的代码看起来很好,但是它会在Marshmallow之后的OS版本的设备上运行,你可以在调试模式下运行app来检查它是否会引发异常。