0

我创建了一个用于播放流式音频文件的媒体播放器服务,我添加了一个带有暂停操作的通知。我想知道如何在我的服务中获取此操作的操作侦听器。

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;

}
4

1 回答 1

0

我确信这个问题可能已经解决,但我遇到了同样的问题,并且能够通过为每个操作添加不同的待处理意图来解决它。这显示在下面的代码中:

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();
}

希望这可以帮助

于 2015-11-25T16:34:40.607 回答