- 在Manifest文件中添加权限声明:
- 创建一个Kotlin类来处理LED灯光,例如EdgeLighting.kt。
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.graphics.Color
import android.os.Build
import android.os.VibrationEffect
import android.os.Vibrator
class EdgeLighting(private val context: Context) {
init {
// 创建通知渠道
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
CHANNEL_ID,
CHANNEL_NAME,
NotificationManager.IMPORTANCE_HIGH)
channel.lightColor = Color.RED
channel.enableLights(true)
channel.enableVibration(true)
val notificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
}
// 触发边缘灯光和振动
fun triggerEdgeLightingAndVibration() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// 触发通知
val notificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(NOTIFICATION_ID, createNotification())
} else {
// 触发边缘灯光和振动
val vibrator = context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
vibrator.vibrate(
VibrationEffect.createOneShot(VIBRATION_DURATION, VibrationEffect.DEFAULT_AMPLITUDE))
} else {
vibrator.vibrate(VIBRATION_DURATION)
}
}
}
// 创建通知
private fun createNotification(): NotificationCompat.Builder {
val intent = Intent(context, MainActivity::class.java)
val pendingIntent = PendingIntent.getActivity(context, 0, intent, 0)
return NotificationCompat.Builder(context, CHANNEL_ID)
.setContentTitle("Edge lighting")
.setContentText("Edge lighting is triggered")
.setSmallIcon(R.drawable.ic_notification)
.setContentIntent(pendingIntent