From 56849d9b25b6ff63ee43f22fe0625ab4d4782e12 Mon Sep 17 00:00:00 2001 From: Sadegh8 Date: Tue, 21 May 2019 02:58:30 +0430 Subject: [PATCH] Update QuizActivity.java Fix android 8 above problems --- .../classicalmusicquiz/QuizActivity.java | 34 ++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/example/android/classicalmusicquiz/QuizActivity.java b/app/src/main/java/com/example/android/classicalmusicquiz/QuizActivity.java index 7b4623f8..dc72a019 100644 --- a/app/src/main/java/com/example/android/classicalmusicquiz/QuizActivity.java +++ b/app/src/main/java/com/example/android/classicalmusicquiz/QuizActivity.java @@ -74,7 +74,7 @@ public class QuizActivity extends AppCompatActivity implements View.OnClickListe private static MediaSessionCompat mMediaSession; private PlaybackStateCompat.Builder mStateBuilder; private NotificationManager mNotificationManager; - + private String CHANNEL_ID = "quiz_playback_channel""; @Override protected void onCreate(Bundle savedInstanceState) { @@ -190,6 +190,29 @@ private Button[] initializeButtons(ArrayList answerSampleIDs) { return buttons; } + /** + *For android 8 above notification + */ + @RequiresApi(Build.VERSION_CODES.O) + private void createChannel() { + NotificationManager + mNotificationManager = + (NotificationManager) this + .getSystemService(Context.NOTIFICATION_SERVICE); + // The id of the channel. + String id = CHANNEL_ID; + // The user-visible name of the channel. + CharSequence name = "Media playback"; + // The user-visible description of the channel. + String description = "Media playback controls"; + int importance = NotificationManager.IMPORTANCE_LOW; + NotificationChannel mChannel = new NotificationChannel(id, name, importance); + // Configure the notification channel. + mChannel.setDescription(description); + mChannel.setShowBadge(false); + mChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC); + mNotificationManager.createNotificationChannel(mChannel); + } /** * Shows Media Style notification, with actions that depend on the current MediaSession @@ -197,8 +220,11 @@ private Button[] initializeButtons(ArrayList answerSampleIDs) { * @param state The PlaybackState of the MediaSession. */ private void showNotification(PlaybackStateCompat state) { - NotificationCompat.Builder builder = new NotificationCompat.Builder(this); - + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + createChannel(); + } + NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID); + int icon; String play_pause; if(state.getState() == PlaybackStateCompat.STATE_PLAYING){ @@ -230,7 +256,7 @@ private void showNotification(PlaybackStateCompat state) { .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) .addAction(restartAction) .addAction(playPauseAction) - .setStyle(new NotificationCompat.MediaStyle() + .setStyle(new android.support.v4.media.app.NotificationCompat.MediaStyle() .setMediaSession(mMediaSession.getSessionToken()) .setShowActionsInCompactView(0,1));