带有服务的通知的addAction监听器

时间:2013-11-02 09:55:23

标签: java android service notifications

我已经创建了一个用于播放流式音频文件的媒体播放器服务,我添加了一个通知,其中包含要暂停的操作。我想知道如何在我的服务中获取此动作的动作监听器。

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;

}

1 个答案:

答案 0 :(得分: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();
}

希望这有帮助