Android 4 及更高版本,多操作通知

标签 android android-intent notifications android-4.2-jelly-bean android-pendingintent

我想在通知中使用三个操作按钮来控制媒体服务,但很难找到代码的工作示例:Vogella 有一个教程,但他使用一个按钮并打开一个 Activity 而不是服务。我已经找到了其他几个此类教程和示例,但没有一个具有三个工作操作按钮,这些按钮具有类似的教程,这些教程演示了服务和多个操作的 Intent 的构造,以及 stackoverflow 上操作按钮使用的最新示例都适用于较低的android 版本,使用单一 Intent 按钮,...

这是我想用来设置通知的代码:

//individual intents for each action???
Intent Back = new Intent(this, MusicService.class );
Back.putExtras("back", 1);
Back.putExtras("play", 0);
Back.putExtras("next", 0);

Intent Play = new Intent (this, MusicService.class);
Back.putExtras("back", 0);
Back.putExtras("play", 1);
Back.putExtras("next", 0);

Intent Next = new Intent (this, MusicService.class);
Next.putExtras("back", 0);
Next.putExtras("play", 0);
Next.putExtras("next", 1);


//do I need to make three pending intents????
PendingIntent pIntentback = PendingIntent.getService(this, 0, Back, 0);
PendingIntent pIntentplay = PendingIntent.getService(this, 0, Play, 0)
PendingIntent pIntentnext = PendingIntent.getService(this, 0, Next, 0)


Notification noti = new Notification.Builder(this)
.setContentTitle("Juke Box Hero")
.setContentText("Playing ")
.setSmallIcon(R.drawable.ic_launcher)
.addAction(R.drawable.back, "", pIntentback)
.addAction(R.drawable.play, "", pIntentplay)
.addAction(R.drawable.next, "", pIntentnext)
.build();

NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

noti.flags = Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, noti);

我有几个问题...

1) 上面的代码是我应该如何设置操作按钮、 Intent 和待定 Intent ?

2) 因为我想接收服务中未决 Intent 所包含的 Intent ,我调用 getService 而不是 getActivity 是否正确?

3) 如果我能够直接在 MusicService 类中接收 Intent ,我如何在不重新启动服务的情况下接收服务中的 Intent (从而丢失已经存储在内存中的数据)?

4) 或者我是否需要构建一个接收器类来扩展 BroadcastReceiver 类并在 list 中注册它,然后将相应的 Intent 发送到 MusicService 类?

请理解我非常愿意研究和编写代码——并且在提出问题之前已经研究了好几天,但我需要这些问题的直接答案...

非常感谢帮助和指导

最佳答案

1) 上面的代码是我应该如何设置操作按钮、 Intent 和待定 Intent ?

回答#1)不,为了设置触发和激活适当 Activity 或服务的未决 Intent , Intent 应该有一个 setAction(在这里插入你的 Action 名称)并且相应的未决 Intent 应该使用更新当前标志未决 Intent (请参阅下面对第 2 条的回答中的示例代码)

Intent 应该是这样的......

Intent Play = new Intent (this, Receiver.class);
Play.setAction("Play");
Play.putExtra("back", 0);
Play.putExtra("play", 1);
Play.putExtra("next", 0);

2) 因为我想接收服务中未决 Intent 所包含的 Intent ,我调用 getService 而不是 getActivity 是否正确?

回答 #2) 不,我使用了 broadcastreciever 类来捕获通知发送的 Intent ;有必要使用 getBroadcast 并确保以下列方式附加应用程序上下文(可能不是必需的,但似乎是个好主意)--

PendingIntent pIntentback = PendingIntent.getBroadcast(this.getApplicationContext(), 0, Play, PendingIntent.FLAG_UPDATE_CURRENT);

3) 如果我能够直接在 MusicService 类中接收 Intent ,我如何在不重新启动服务的情况下接收服务中的 Intent (从而丢失已经存储在内存中的数据)?

回答#3)我回避了这个问题,但在对这个想法进行了一些研究之后,您似乎可以在服务类中构建一个接收器来处理这种情况......我现在无法提供详细信息这样的效果和要求,但是我使用的外部广播接收器系统不会中断音乐服务或重置已经通过原始服务 Intent 传递的信息......

4) 或者我是否需要构建一个接收器类来扩展 BroadcastReceiver 类并在 list 中注册它,然后将相应的 Intent 发送到 MusicService 类?

回答 #4)如上所述,构建服务外部的接收者类不是必需的,但在不损坏功能完善的服务类的情况下似乎最容易操作……接收者类中的个人 Intent 是通过附加的操作键识别,然后构建所需的操作特定 Intent 以发送到服务类...

关于Android 4 及更高版本,多操作通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18758616/

相关文章:

android - 开源/免版税 android 控件的网站

java - Android:putExtra parcelable 停止启动 Activity 。没有错误

java - Android 每日通知在错误的时间多次显示

python - 如何从 pynotify.Notification 中读取标题、文本和图标名称?

java - 查询 Android 浏览器历史记录中的 URL

php - 如何在 Laravel 中通知用户作业已完成?

android - 在手机浏览器上单击图标时显示边框

java - 为Android shell编译终端应用程序以记录当前时间?

android - 忽略 Alpha 并覆盖目标的 Porter Duff 模式

java - 此应用程序小部件 fragment 中所有 Intent (PendingIntents)的逻辑流