如何在android中的通知中添加按钮?

2023-09-08移动开发问题
87

本文介绍了如何在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中的通知中添加按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

硬件音量按钮更改应用程序音量
Hardware Volume buttons change in app volume(硬件音量按钮更改应用程序音量)...
2024-08-12 移动开发问题
10

沙盒游戏中心回合事件通知不一致
Sandbox Game Center Turn Event Notifications Not Consistent(沙盒游戏中心回合事件通知不一致)...
2024-08-12 移动开发问题
4

如何从 CCScrollView 上的按钮更改标签文本?
How to change labels text from a button on a CCScrollView?(如何从 CCScrollView 上的按钮更改标签文本?)...
2024-08-12 移动开发问题
6

如何在 COCOS2d Android 中使用 CClistview?
How can I use CClistview in COCOS2d Android?(如何在 COCOS2d Android 中使用 CClistview?)...
2024-08-12 移动开发问题
5

在 iphone 中解析推送通知
Parse push notification in iphone(在 iphone 中解析推送通知)...
2024-08-12 移动开发问题
10

cocos2d-android:如何显示分数
cocos2d-android: how to display score(cocos2d-android:如何显示分数)...
2024-08-11 移动开发问题
7