简体   繁体   English

使用服务通知时的addAction侦听器

[英]addAction listener on notification with service

I've created a media player service for playing streamed audio file, i've added a notification with an action to pause. 我已经创建了用于播放流音频文件的媒体播放器服务,还添加了带有暂停操作的通知。 I would like to know how to get the action listener of this action in my service. 我想知道如何在我的服务中获取此动作的动作侦听器。

MediaPlayerService.java MediaPlayerService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(this, MainActivity.class), 0);

    notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    noti = new Notification.Builder(getApplicationContext())
            .setOngoing(true)
            .setContentTitle("Lecture")
            .setTicker("Lecture d'un message en cours")
            .setContentText("Message audio en cours")
            .setContentIntent(pendingIntent)
            .setSmallIcon(R.drawable.ic_launcher)
            .addAction(android.R.drawable.ic_media_pause, "Pause", pendingIntent);

    notificationManager.notify(0, noti.build());

    return START_STICKY;

}

i am sure this question may have been resolved but I had the same issue and was able to resolve it by adding a different pending intent for every action. 我确定这个问题可能已经解决,但是我遇到了同样的问题,并且能够通过为每个操作添加不同的待处理意图来解决此问题。 This is shown in the code below: 如下代码所示:

private static Notification buildNotification(Context context, String filename, int recordFlag,boolean stopFlag,boolean playFlag) {
    PendingIntent pIntent = createPendingIntent(REQUEST_CODE_ACTIVIY, context, false);
    PendingIntent delIntent = getDeleteIntent(context);
    // Build notification
    // Actions are just fake
    int drawRecord =R.drawable.ic_record_audio;

    Notification.Builder builder = new Notification.Builder(context.getApplicationContext())
            .setContentTitle(RECORD_AUDIO)
            .setContentText(filename).setSmallIcon(R.drawable.ic_audio_list)
            .setContentIntent(pIntent)
            .setDeleteIntent(delIntent);


        builder = builder.addAction(drawRecord, RECORD, createPendingIntent(REQUEST_CODE_SERVICE_RECORD, context, true));
        builder = builder.addAction(R.drawable.ic_stop_white, STOP, createPendingIntent(REQUEST_CODE_SERVICE_STOP, context, true));

        builder = builder.addAction(R.drawable.ic_play_white, PLAY, createPendingIntent(REQUEST_CODE_SERVICE_PLAY, context, true));


    return builder.build();
}

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM