如何在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中執行應用程式,請開啟您的一個專案活動檔案,然後單擊執行圖示 工具欄中的。選擇您的移動裝置作為選項,然後檢查您的移動裝置,它將顯示您的預設螢幕
注意:建議在真機上測試以獲得更好的結果
點選 這裡 下載專案程式碼。
廣告