2016-09-24 70 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

我發現在我自己的情況下,只是不確定這是否正確。謝謝您的幫助!!! –