通知按钮无响应

时间:2015-10-01 11:12:41

标签: android service android-notifications android-music-player

我有以下代码,可以在用户开始播放音乐时创建通知;

private Notification buildNotification(String title, String artist, Bitmap art) {
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext());
    notificationBuilder.setOngoing(true);
    notificationBuilder.setAutoCancel(true);  //TODO revert this to false depending on effect
    notificationBuilder.setSmallIcon(R.drawable.ic_stat_playing);

    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 1,
            new Intent(getApplicationContext(), MusicPlayer.class), PendingIntent.FLAG_UPDATE_CURRENT);
    notificationBuilder.setContentIntent(pendingIntent);

    RemoteViews compactNotification = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notfication_custom_layout);
    RemoteViews expandedNotification = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification_expanded_layout);

    Intent previousTrackIntent = new Intent(getApplicationContext(), MusicService.class);
    previousTrackIntent.setAction(MusicService.ACTION_REWIND);
    PendingIntent previousTrackPendingIntent = PendingIntent.getService(getApplicationContext(), 1, previousTrackIntent, 0);

    Intent playPauseTrackIntent = new Intent(getApplicationContext(), MusicService.class);
    playPauseTrackIntent.setAction(MusicService.ACTION_TOGGLE_PLAYBACK);
    PendingIntent playPauseTrackPendingIntent = PendingIntent.getService(getApplicationContext(), 1, playPauseTrackIntent, 0);

    Intent nextTrackIntent = new Intent(getApplicationContext(), MusicService.class);
    nextTrackIntent.setAction(MusicService.ACTION_SKIP);
    PendingIntent nextTrackPendingIntent = PendingIntent.getService(getApplicationContext(), 1, nextTrackIntent, 0);

    Intent stopServiceIntent = new Intent(getApplicationContext(), MusicService.class);
    stopServiceIntent.setAction(MusicService.ACTION_STOP);
    PendingIntent stopServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1, stopServiceIntent, 0);

    if (isPlayingMusic()) {
        compactNotification.setImageViewResource(R.id.notification_base_play, R.drawable.ic_action_pause_dark);
        expandedNotification.setImageViewResource(R.id.notification_expanded_base_play, R.drawable.ic_action_pause_dark);
    } else {
        compactNotification.setImageViewResource(R.id.notification_base_play, R.drawable.ic_action_play_dark);
        expandedNotification.setImageViewResource(R.id.notification_expanded_base_play, R.drawable.ic_action_play_dark);
    }

    compactNotification.setTextViewText(R.id.notification_base_line_one, title);
    compactNotification.setTextViewText(R.id.notification_base_line_two, artist);

    expandedNotification.setTextViewText(R.id.notification_expanded_base_line_one, title);
    expandedNotification.setTextViewText(R.id.notification_expanded_base_line_two, artist);

    compactNotification.setOnClickPendingIntent(R.id.notification_base_play, playPauseTrackPendingIntent);
    expandedNotification.setOnClickPendingIntent(R.id.notification_expanded_base_play, playPauseTrackPendingIntent);

    compactNotification.setOnClickPendingIntent(R.id.notification_base_next, nextTrackPendingIntent);
    expandedNotification.setOnClickPendingIntent(R.id.notification_expanded_base_next, nextTrackPendingIntent);

    compactNotification.setOnClickPendingIntent(R.id.notification_base_previous, previousTrackPendingIntent);
    expandedNotification.setOnClickPendingIntent(R.id.notification_expanded_base_previous, previousTrackPendingIntent);

    compactNotification.setOnClickPendingIntent(R.id.notification_base_collapse, stopServicePendingIntent);
    expandedNotification.setOnClickPendingIntent(R.id.notification_expanded_base_collapse, stopServicePendingIntent);

    expandedNotification.setImageViewBitmap(R.id.notification_expanded_base_image, art);
    compactNotification.setImageViewBitmap(R.id.notification_base_image, art);

    notificationBuilder.setContent(compactNotification);

    Notification notification = notificationBuilder.build();

    if (Build.VERSION.SDK_INT >= 16)
        notification.bigContentView = expandedNotification;

    notification.flags = Notification.FLAG_FOREGROUND_SERVICE | Notification.FLAG_ONGOING_EVENT;

    return notification;
}

会显示通知,但有一些问题:

  • 按下关闭按钮时,会成功调用stopServiceIntent并停止播放,但不会取消通知。
  • 当从播放器切换播放时,播放按钮不会根据当前状态改变,即暂停时,它应该改变为播放按钮,播放时应该改为暂停按钮。

应该切换按钮的代码是:

public boolean isPlayingMusic() {
    try {
        return mPlayer.isPlaying();
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

停止播放时应取消通知的代码是:

private void cancelNotification() {
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.cancel(NOTIFICATION_ID);
}

0 个答案:

没有答案