Androidは懐中電灯の機能をオンにします。(Android 4 x、5 x、6 xにぴったりです。)
8767 ワード
最近はAndroid 4 x、5 x、6 xの懐中電灯の開閉方法を研究しています。公共の方法を書いて、皆さんに分かち合います。
カメラとフラッシュの権限を追加
カメラとフラッシュの権限を追加
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.autofocus" />
レイアウトファイル<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
<Button android:id="@+id/btn_open" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:text=" " />
<Button android:id="@+id/btn_close" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" " />
</LinearLayout>
具体的に実現する/** * Created by Castiel * :http://blog.csdn.net/mynameishuangshuai */
public class MainActivity extends AppCompatActivity {
private CameraManager manager;// CameraManager
private Camera m_Camera = null;// Camera
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnOpen = (Button) findViewById(R.id.btn_open);
Button btnClose = (Button) findViewById(R.id.btn_close);
manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
String[] camerList = manager.getCameraIdList();
for (String str : camerList) {
}
} catch (CameraAccessException e) {
Log.e("error", e.getMessage());
}
btnOpen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
lightSwitch(false);
}
});
btnClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
lightSwitch(true);
}
});
}
/** * * * @param lightStatus * @return */
private void lightSwitch(final boolean lightStatus) {
if (lightStatus) { //
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
try {
manager.setTorchMode("0", false);
} catch (Exception e) {
e.printStackTrace();
}
} else {
if (m_Camera != null) {
m_Camera.stopPreview();
m_Camera.release();
m_Camera = null;
}
}
} else { //
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
try {
manager.setTorchMode("0", true);
} catch (Exception e) {
e.printStackTrace();
}
} else {
final PackageManager pm = getPackageManager();
final FeatureInfo[] features = pm.getSystemAvailableFeatures();
for (final FeatureInfo f : features) {
if (PackageManager.FEATURE_CAMERA_FLASH.equals(f.name)) { //
if (null == m_Camera) {
m_Camera = Camera.open();
}
final Camera.Parameters parameters = m_Camera.getParameters();
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
m_Camera.setParameters(parameters);
m_Camera.startPreview();
}
}
}
}
}
/** * Android >= M(API23) */
private boolean isM() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return true;
} else {
return false;
}
}
}