如何在android中的通知中添加按钮? [英] How to add button to notifications in android?

查看:36
本文介绍了如何在android中的通知中添加按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用播放音乐,当用户通过从屏幕顶部(或通常从平板电脑屏幕的右下角)滑动打开通知屏幕时,我想向他们展示一个按钮来停止当前播放的音乐并开始如果他们愿意,可以再做一次.

My app plays music and when users open notifications screen by swiping from the top of the screen ( or generally from the bottom right of the screen on tablets ), I want to present them a button to stop the currently playing music and start it again if they want.

我不打算将小部件放在用户的主屏幕上,而只是放在通知中.我该怎么做?

I am not planning to put a widget on the user's home screen, but just into notifications. How can I do this?

推荐答案

您可以为动作创建一个意图(在这种情况下停止播放),然后将其作为动作按钮添加到您的通知中.

You can create an intent for the action (in this case stop playing) and then add it as an action button to your notification.

Intent snoozeIntent = new Intent(this, MyBroadcastReceiver.class);
snoozeIntent.setAction(ACTION_SNOOZE);
snoozeIntent.putExtra(EXTRA_NOTIFICATION_ID, 0);
PendingIntent snoozePendingIntent =
        PendingIntent.getBroadcast(this, 0, snoozeIntent, 0);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("My notification")
        .setContentText("Hello World!")
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
        .setContentIntent(pendingIntent)
        .addAction(R.drawable.ic_snooze, getString(R.string.snooze),
                snoozePendingIntent);

请参阅Android 文档.

这篇关于如何在android中的通知中添加按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆