Android通知操作未调用待处理意图到服务

9

我正在尝试为我的持续通知添加一个按钮,当点击它时,它应该调用控制音频播放的服务。

这是我的通知和意图:

        Intent intent = new Intent(context, AudioStreamService.class);


    Random generator = new Random();


    PendingIntent i = PendingIntent.getActivity(context, 579, intent,PendingIntent.FLAG_UPDATE_CURRENT);


    final NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle(title)
            .setContentText(text)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setLargeIcon(picture)
            .setTicker(ticker)
            .setNumber(number)
            .setOngoing(true)
            .addAction(
               R.drawable.ic_action_stat_reply,
               res.getString(R.string.action_reply),
               i);

    notify(context, builder.build());

这是我的服务启动代码:

    public int onStartCommand(Intent intent, int flags, int startId) {
    Log.e("APP ID", "APP ID - service called ");

    if(isPlaying){
        stop();
    }
    else {
        play();
    }
    return Service.START_STICKY;
}

当从通知中的操作调用时,日志永远不会被触发。但是该服务由应用程序中的按钮使用,并且正常工作。日志像下面的命令一样被调用。

1个回答

21

使用:

PendingIntent pendingIntent = PendingIntent.getService(context, 579, intent,PendingIntent.FLAG_UPDATE_CURRENT);

而不是:

PendingIntent pendingIntent = PendingIntent.getActivity(context, 579, intent,PendingIntent.FLAG_UPDATE_CURRENT);

^^ 这可用于从通知中启动活动。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接