我构建了一个小应用程序,它有提醒并在您的提醒被触发时发送通知,我想添加的是全屏通知(或触发警报时弹出的活动)用户的手机是否关闭,锁定,管他呢。安卓手机的默认闹钟应用程序的想法相同。我到处搜索,youtube,google 等。但没有找到任何东西
我尝试使用 setFullScreenIntent(),但只显示正常通知。
这是我在主要活动中的通知渠道代码:
private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
var sound = Uri.parse("android.resource://" + applicationContext.packageName + "/" + R.raw.siren);
val soundAttributes = AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_ALARM)
.build()
val name : CharSequence = "ReminderAlarm"
val description = "Channel for alarm manager"
val importance = NotificationManager.IMPORTANCE_HIGH
val notificationChannel = NotificationChannel("androidAlarm4", name, importance)
notificationChannel.description = description
notificationChannel.setSound(sound, soundAttributes)
val manager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
manager.createNotificationChannel(notificationChannel)
Log.e("notification: ", "created a notification ")
}
}
我在 AddActivity 中添加警报类:
private fun setAlarm() {
alarmManager = getSystemService(ALARM_SERVICE) as AlarmManager
val intent = Intent(this, AlarmReceiver::class.java)
val pendingIntent = PendingIntent.getBroadcast(this, (0..19991).random(), intent, PendingIntent.FLAG_IMMUTABLE)
calendar.set(Calendar.HOUR_OF_DAY, editTime.hour)
calendar.set(Calendar.MINUTE, editTime.minute)
calendar.set(Calendar.SECOND, 0)
if (repeatSwitch.isChecked){
alarmManager.setRepeating(
AlarmManager.RTC_WAKEUP,
calendar.timeInMillis,
AlarmManager.INTERVAL_DAY,
pendingIntent
)
Toast.makeText(this, "Repeating alarm set", Toast.LENGTH_SHORT).show()
Log.e("notification: ", "${pendingIntent.hashCode()}")
}else {
alarmManager.setExactAndAllowWhileIdle(
AlarmManager.RTC_WAKEUP,
calendar.timeInMillis,
pendingIntent
)
Toast.makeText(this, "alarm set", Toast.LENGTH_SHORT).show()
Log.e("notification: ", "${pendingIntent.hashCode()}")
}
}
我的报警接收器类:
private lateinit var pendingIntent: PendingIntent
private lateinit var fullScreenPendingIntent: PendingIntent
class AlarmReceiver : BroadcastReceiver() {
val notificationId = 2
val channelID = "androidAlarm4"
override fun onReceive(context: Context?, intent: Intent?) {
pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE)
val fullScreenNotification = Intent(context, AlarmPage::class.java)
fullScreenPendingIntent = PendingIntent.getActivity(context, 0, fullScreenNotification,PendingIntent.FLAG_IMMUTABLE)
var sound = Uri.parse("android.resource://" + context?.packageName + "/" + R.raw.siren);
val notification = NotificationCompat.Builder(context!!, channelID)
.setSmallIcon(drawable.notification_bg)
.setContentTitle("Reminder OFF")
.setContentText("Work has to be done now!!")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setSound(sound)
.setAutoCancel(true)
.setCategory(NotificationCompat.CATEGORY_ALARM)
.setFullScreenIntent(pendingIntent, true)
.build()
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(notificationId, notification)
Log.e("notification: ", "received a notification: ")
}
}
闹钟关闭应该弹出的页面是空的,不知道要不要加代码,就是这个页面:
class AlarmPage : AppCompatActivity() {
private lateinit var stopReminder : Button
@RequiresApi(Build.VERSION_CODES.O)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_alarm_page)
init()
stopReminder.setOnClickListener {
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
}
}
private fun init(){
stopReminder = findViewById(R.id.stopReminder)
}
}
我不知道出了什么问题..