如何在Android中使用Kotlin编程方式打开手电筒?
此示例演示如何使用Kotlin在Android中以编程方式打开手电筒。
步骤1 - 在Android Studio中创建一个新项目,转到文件?新建项目并填写所有必需的详细信息以创建新项目。
步骤2 - 将以下代码添加到res/layout/activity_main.xml
示例
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="50dp" android:text="Tutorials Point" android:textAlignment="center" android:textColor="@android:color/holo_green_dark" android:textSize="32sp" android:textStyle="bold" /> <ToggleButton android:id="@+id/onOffFlashlight" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:checked="false" android:text="Turn On/Off Camera LED/ Flashlight Android" android:textOff="Turn On" android:textOn="Turn Off" /> </RelativeLayout>
步骤3 - 将以下代码添加到src/MainActivity.kt
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.ToggleButton
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private lateinit var cameraManager: CameraManager
private lateinit var cameraId: String
private lateinit var toggleButton: ToggleButton
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
val isFlashAvailable = applicationContext.packageManager
.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT)
if (!isFlashAvailable) {
showNoFlashError()
}
cameraManager = getSystemService(Context.CAMERA_SERVICE) as CameraManager
try {
cameraId = cameraManager.cameraIdList[0]
} catch (e: CameraAccessException) {
e.printStackTrace()
}
toggleButton = findViewById(R.id.onOffFlashlight)
toggleButton.setOnCheckedChangeListener { _, isChecked -> switchFlashLight(isChecked) }
}
private fun showNoFlashError() {
val alert = AlertDialog.Builder(this)
.create()
alert.setTitle("Oops!")
alert.setMessage("Flash not available in this device...")
alert.setButton(DialogInterface.BUTTON_POSITIVE, "OK") { _, _ -> finish() }
alert.show()
}
private fun switchFlashLight(status: Boolean) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
cameraManager.setTorchMode(cameraId, status)
}
} catch (e: CameraAccessException) {
e.printStackTrace()
}
}
}步骤4 - 将以下代码添加到androidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.q11"> <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>
让我们尝试运行您的应用程序。我假设您已将您的实际Android移动设备连接到您的计算机。要在Android Studio中运行应用程序,请打开您的一个项目活动文件,然后单击运行图标
工具栏中的。选择您的移动设备作为选项,然后检查您的移动设备,它将显示您的默认屏幕
注意:建议在真机上测试以获得更好的结果


点击 这里 下载项目代码。
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP