2016-09-24 3 views
0

私はメディアプレーヤーの通知を作成する際に問題に直面しています。基本的には、通知ビルダーを使用してメディアプレーヤーのリモートコントロールを構築しようとしています。私はNotificationCompat Libraryを使用して、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の宣言を次のように変更してください:

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

私はそれが正しいかどうかはわかりませんでした。助けてくれてありがとう!!! –

関連する問題