如何解决单击自定义通知操作按钮后显示对话框
我使用了以下代码并且能够在 Android 应用中显示自定义通知。
MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Create and register notification channel api 26+
val channelId = "My_Channel_ID"
val notificationId = 1
createNotificationChannel(channelId)
// Create an explicit intent for an activity in this app
val intent = Intent(this,MainActivity::class.java)
intent.apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
val pendingIntent = PendingIntent.getActivity(this,intent,0)
val deleteIntent = Intent(this,BharosaBroadcastReceiver::class.java)
deleteIntent.apply {
action = "Delete"
putExtra("UserId","100")
putExtra("notificationId",notificationId)
}
val deletePendingIntent = PendingIntent.getBroadcast(this,deleteIntent,0)
var btn : Button = findViewById(R.id.btn)
btn.setOnClickListener{
val contentView = RemoteViews(packageName,R.layout.custom_push)
contentView.setImageViewResource(R.id.image,R.mipmap.ic_launcher)
contentView.setTextViewText(R.id.title,"Hello World")
contentView.setTextViewText(R.id.text,"Please click the delete btton to delete")
val notificationBuilder = NotificationCompat.Builder(this,channelId)
.setSmallIcon(R.drawable.ic_action_info)
.setColor(Color.GREEN)
.setCustomContentView(contentView)
.setStyle(NotificationCompat.DecoratedCustomViewStyle())
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
contentView.setOnClickPendingIntent(R.id.delete,deletePendingIntent)
with(NotificationManagerCompat.from(this)){
notify(notificationId,notificationBuilder.build())
}
}
}
private fun createNotificationChannel(channelId:String) {
// Create the NotificationChannel,but only on API 26+ (Android 8.0) because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
val name = "My Channel"
val channelDescription = "Channel Description"
val importance = NotificationManager.IMPORTANCE_DEFAULT
val channel = NotificationChannel(channelId,name,importance)
channel.apply {
description = channelDescription
}
// Finally register the channel with system
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
}
}
MyBroadcastReceiver.kt
class MyBroadcastReceiver: BroadcastReceiver(){
override fun onReceive(context: Context?,intent: Intent?) {
intent?.apply {
val ussreId = getStringExtra("UserId")
val notificationId = getIntExtra("notificationId",0)
Toast.makeText(context,"Deleted ID: $ussreId",Toast.LENGTH_SHORT).show()
context?.apply {
// Remove the notification programmatically on button click
NotificationManagerCompat.from(this).cancel(notificationId)
}
}
}
}
对话框的代码如下:
var dialog = AlertDialog.Builder(requireContext())
dialog.setTitle("Delete?")
dialog.setMessage("Please confirm to delete it")
dialog.setPositiveButton("Delete",DialogInterface.OnClickListener { dialog,whichButton ->
})
dialog.setNegativeButton("No",null)
var dialogUI = dialog.create();
dialogUI.setOnShowListener {
val view = (it as AlertDialog).window
view?.setBackgroundDrawableResource(R.drawable.alert_dialog_layout)
// change positive button yes background
val positiveButton: Button = (it as AlertDialog).getButton(DialogInterface.BUTTON_POSITIVE)
positiveButton.background = resources.getDrawable(R.drawable.yes_alert_background)
positiveButton.setTextColor(requireContext().resources.getColor(R.color.white))
val negativeButton = it.getButton(DialogInterface.BUTTON_NEGATIVE)
negativeButton.setTextColor(requireContext().resources.getColor(R.color.dialog_no_text))
}
dialogUI.show()
请帮助我在单击自定义通知操作按钮后显示此对话框。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。