7. How to turn on Flash light
페이지 정보
작성자 관리자 댓글 0건 조회 3,036회 작성일 20-05-11 21:40본문
7. How to turn on Flash light
1. 새 프로젝트 생성
프로젝트명 : FlashLightTest2
2. 화면 디자인 수정
<?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:gravity="center"
android:orientation="vertical">
<ToggleButton
android:id="@+id/onOffFlashlight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="Turn Off"
android:textOff="Turn On"
android:checked="false"
android:text="Turn On/Off Camera LED/ Flashlight Android" />
</LinearLayout>
3. MainActivity.kt 수정
package kr.co.leelab.flashlighttest2
import android.app.AlertDialog
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.widget.CompoundButton
import android.widget.ToggleButton
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private var mCameraManager: CameraManager? = null
private var mCameraId: String? = null
private var toggleButton: ToggleButton? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val isFlashAvailable = applicationContext.packageManager
.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT)
if (!isFlashAvailable) {
showNoFlashError()
}
mCameraManager = getSystemService(Context.CAMERA_SERVICE) as CameraManager
try {
mCameraId = mCameraManager!!.getCameraIdList()[0]
} catch (e: CameraAccessException) {
e.printStackTrace()
}
toggleButton = findViewById(R.id.onOffFlashlight)
toggleButton!!.setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener {
buttonView, isChecked ->
switchFlashLight(isChecked)
})
}
fun showNoFlashError() {
val alert: AlertDialog = AlertDialog.Builder(this)
.create()
alert.setTitle("Oops!")
alert.setMessage("Flash not available in this device...")
alert.setButton(DialogInterface.BUTTON_POSITIVE, "OK", DialogInterface.OnClickListener { dialog, which -> finish() })
alert.show()
}
fun switchFlashLight(status: Boolean) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mCameraManager!!.setTorchMode(mCameraId!!, status)
}
} catch (e: CameraAccessException) {
e.printStackTrace()
}
}
}
4. 권한설정
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="kr.co.leelab.flashlighttest2">
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera.flash" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
5. 실행
댓글목록
등록된 댓글이 없습니다.