我正在尝试制作一个音乐播放器应用程序。通知中我需要三个按钮。我正在发送附加的通知的意图。但不知何故他们永远也达不到。下面是我的代码,我想我需要以某种方式放弃以前的附加程序,或者更改挂起意图的标志,不太确定。
void showNotificationForeground() {
Intent i = new Intent(getApplicationContext(), PlayerService.class);
i.putExtra("name", name);
i.putExtra("songname", songname);
i.putExtra("isservice", true);
PendingIntent pi = PendingIntent.getService(getApplicationContext(), 0,
i, 0);
Intent ipause = new Intent(getApplicationContext(), PlayerService.class);
ipause.putExtra("name", name);
ipause.putExtra("songname", songname);
ipause.putExtra("isservice", true);
ipause.putExtra("action", "pause");
PendingIntent pipause = PendingIntent.getService(
getApplicationContext(), 0, ipause, 0);
Intent iplay = new Intent(getApplicationContext(), PlayerService.class);
iplay.putExtra("name", name);
iplay.putExtra("songname", songname);
iplay.putExtra("isservice", true);
iplay.putExtra("action", "pause");
PendingIntent piplay = PendingIntent.getService(
getApplicationContext(), 0, iplay, 0);
Intent istop = new Intent(getApplicationContext(), PlayerService.class);
istop.putExtra("name", name);
istop.putExtra("songname", songname);
istop.putExtra("isservice", true);
istop.putExtra("action", "pause");
PendingIntent pistop = PendingIntent.getService(
getApplicationContext(), 0, istop, 0);
//
Notification noti = new Notification.Builder(this)
.setContentTitle("RajPlayer")
.setContentText("Playing: " + songname)
.setSmallIcon(R.drawable.ic_launcher).setContentIntent(pi)
.addAction(R.drawable.play96, "Play", piplay)
.addAction(R.drawable.pause96, "Pause", pipause)
.addAction(R.drawable.cancel24, "Stop", pistop).build();
//
startForeground(NOTIFICATION_ID, noti);
}发布于 2014-09-02 12:09:18
意图之间的比较不考虑额外因素,正如您可以看到的这里。
所以可能会发生的情况是,一旦第一个pendingIntent被创建,安卓就会回收它,因为它相信它们是相同的--而且因为第一个没有额外的动作,所以你没有得到它。
验证这种情况的最快方法是给你所有的动作一个动作,就像这样,只是为了区别它们。
ipause.setAction("justSomething");
iplay.setAction("toMakeTheIntents");
istop.setAction("NotEqual")我相信这能解决你的问题。
另外,对于所有的意图,您都有相同的动作(“暂停”)。可能是错误的复制/粘贴。但这不是问题,就像你说的,你根本得不到行动。
https://stackoverflow.com/questions/25622386
复制相似问题