首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用三个按钮将附加信息传递给服务

用三个按钮将附加信息传递给服务
EN

Stack Overflow用户
提问于 2014-09-02 11:31:40
回答 1查看 286关注 0票数 0

我正在尝试制作一个音乐播放器应用程序。通知中我需要三个按钮。我正在发送附加的通知的意图。但不知何故他们永远也达不到。下面是我的代码,我想我需要以某种方式放弃以前的附加程序,或者更改挂起意图的标志,不太确定。

代码语言:javascript
运行
复制
   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);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-02 12:09:18

意图之间的比较不考虑额外因素,正如您可以看到的这里

所以可能会发生的情况是,一旦第一个pendingIntent被创建,安卓就会回收它,因为它相信它们是相同的--而且因为第一个没有额外的动作,所以你没有得到它。

验证这种情况的最快方法是给你所有的动作一个动作,就像这样,只是为了区别它们。

代码语言:javascript
运行
复制
ipause.setAction("justSomething");
iplay.setAction("toMakeTheIntents");
istop.setAction("NotEqual")

我相信这能解决你的问题。

另外,对于所有的意图,您都有相同的动作(“暂停”)。可能是错误的复制/粘贴。但这不是问题,就像你说的,你根本得不到行动。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25622386

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档