2016-09-24 55 views
0

我在为媒体播放器创建通知时遇到问题。基本上我正在尝试使用通知构建器为我的媒体播放器构建远程控制。我正在使用NotificationCompat库来支持较低版本的android。下面是代码的面临哪些错误的部分:NotificationCompat Builder错误

private NotificationCompat.Action createAction(int iconResId, String title, String action){ 
    Intent intent = new Intent(this, MusicBoxService.class); 
    intent.setAction(action); 
    PendingIntent pendingIntent = PendingIntent.getService(MainApplication.getContext(), 1, intent, 0); 
    return new NotificationCompat.Action.Builder(iconResId, title, pendingIntent).build(); 
} 

private void updateNotification(){ 
    NotificationCompat.Action playPauseAction = playbackState.getState() == playbackState.STATE_PLAYING ? 
      createAction(R.drawable.ic_pause_black_18dp, "Pause", ACTION_PAUSE): 
      createAction(R.drawable.ic_play_arrow_black_18dp, "Play", ACTION_PLAY); 

    NotificationCompat notificationCompat = new NotificationCompat.Builder(this) 
      .setPriority(NotificationCompat.PRIORITY_DEFAULT) 
      .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) 
      .setCategory(NotificationCompat.CATEGORY_TRANSPORT) 
      .setContentTitle(title) 
      .setContentText(artist) 
      .setOngoing(playbackState.getState() == playbackState.STATE_PLAYING) 
      .setShowWhen(false) 
      .setSmallIcon(R.drawable.ic_music_box_black_18dp) 
      .setAutoCancel(false) 
      .addAction(createAction(R.drawable.ic_skip_previous_black_18dp, "Previous", ACTION_PREV)) 
      .addAction(playPauseAction) 
      .addAction(createAction(R.drawable.ic_skip_next_black_18dp, "Next", ACTION_NEXT)) 
      .setStyle(new NotificationCompat.MediaStyle().setMediaSession(mMediaSession.getSessionToken()).setShowActionsInCompactView(1,2)) 
        .build(); 
    ((NotificationManagerCompat)getSystemService(Context.NOTIFICATION_SERVICE)).notify(1, notificationCompat); 
} 

以下是错误的截图: enter image description here

的事情是,转产NotificationCompat notificationCompat =新... [通知notificationCompat = new ..解决了错误,但我不确定这是否正确。

Android Studio中给出了错误: 要求:android.support.v7.app.NotificationCompat 发现:android.app.Notification

谢谢! 任何帮助,非常感谢。

回答

2

NotificationCompat仅用于构建Notification。使用NotificationCompat.Builder建立通知仍会返回android.app.Notification,而不是NotificationCompat。只要将您的notificationCompat的声明更改为:

Notification notification = new NotificationCompat.Builder(this) 
     ... 
     .build(); 
+1

我发现在我自己的情况下,只是不确定这是否正确。谢谢您的帮助!!! –