List of usage examples for android.app Notification.Builder setSmallIcon
@UnsupportedAppUsage public void setSmallIcon(Icon icon)
From source file:audio.lisn.service.MediaNotificationManager.java
private Notification createNotification() { // LogHelper.d(TAG, "updateNotificationMetadata. mMetadata=" + mMetadata); // if (mMetadata == null || mPlaybackState == null) { // return null; // }/*from w w w . j a v a 2 s .co m*/ Notification.Builder notificationBuilder = new Notification.Builder(mService); int playPauseButtonPosition = 1; // If skip to previous action is enabled notificationBuilder.addAction(R.drawable.ic_play_skip_previous, mService.getString(R.string.label_previous), mPreviousIntent); /* if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN){ notificationBuilder.addAction(new Notification.Action( R.drawable.ic_play_skip_previous, mService.getString(R.string.label_previous), mPreviousIntent)); }else{ notificationBuilder.addAction(R.drawable.ic_play_skip_previous, mService.getString(R.string.label_previous), mPreviousIntent); } */ // If there is a "skip to previous" button, the play/pause button will // be the second one. We need to keep track of it, because the MediaStyle notification // requires to specify the index of the buttons (actions) that should be visible // when in compact view. // addPlayPauseAction(notificationBuilder); // If skip to next action is enabled notificationBuilder.addAction(R.drawable.ic_play_skip_next, mService.getString(R.string.label_next), mNextIntent); /* if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN) { notificationBuilder.addAction(new Notification.Action(R.drawable.ic_play_skip_next, mService.getString(R.string.label_next), mNextIntent)); }else{ notificationBuilder.addAction(R.drawable.ic_play_skip_next, mService.getString(R.string.label_next), mNextIntent); } */ AudioBook audioBook = AppController.getInstance().getCurrentAudioBook(); Bitmap art = null; /* String img_path = AppUtils.getDataDirectory(mService.getApplicationContext()) + audioBook.getBook_id()+ File.separator+"book_cover.jpg"; File imgFile = new File(img_path); if(imgFile.exists()) { BitmapFactory.Options bmOptions = new BitmapFactory.Options(); art = BitmapFactory.decodeFile(imgFile.getAbsolutePath(), bmOptions); }else{ art = BitmapFactory.decodeResource(mService.getResources(), R.drawable.ui_bg_logo); } */ art = BitmapFactory.decodeResource(mService.getResources(), R.drawable.ic_notification_large); notificationBuilder.setSmallIcon(R.drawable.ic_notification).setUsesChronometer(true) .setContentIntent(createContentIntent()).setContentTitle(audioBook.getEnglish_title()) .setDeleteIntent(mDeleteIntent) .setContentText(AppController.getInstance().getPlayerControllerTitle()).setLargeIcon(art); if (Build.VERSION.SDK_INT >= 21) { notificationBuilder.setStyle( new Notification.MediaStyle().setShowActionsInCompactView(new int[] { playPauseButtonPosition }) // show only play/pause in compact view ).setVisibility(Notification.VISIBILITY_PUBLIC).setColor(mNotificationColor) ; } setNotificationPlaybackState(notificationBuilder); return notificationBuilder.build(); }
From source file:com.massivcode.androidmusicplayer.services.MusicService.java
private void showNotificationUnderLollipop() { Notification.Builder builder = new Notification.Builder(this); RemoteViews remoteViews = new RemoteViews(this.getPackageName(), R.layout.notification); // ? ? ? //from w w w.j a va 2 s . c om // ========================================================================================= Intent musicPreviousIntent = new Intent(getApplicationContext(), MusicService.class); musicPreviousIntent.setAction(ACTION_PLAY_PREVIOUS); musicPreviousIntent.putExtra("position", getPositionAtPreviousOrNext(ACTION_PLAY_PREVIOUS)); PendingIntent musicPreviousPendingIntent = PendingIntent.getService(getApplicationContext(), 0, musicPreviousIntent, PendingIntent.FLAG_UPDATE_CURRENT); remoteViews.setOnClickPendingIntent(R.id.noti_previous_ib, musicPreviousPendingIntent); // ========================================================================================= // ? ? ? // ========================================================================================= Intent musicStopIntent = new Intent(getApplicationContext(), MusicService.class); musicStopIntent.setAction(ACTION_PAUSE); PendingIntent musicStopPendingIntent = PendingIntent.getService(getApplicationContext(), 1, musicStopIntent, PendingIntent.FLAG_UPDATE_CURRENT); remoteViews.setOnClickPendingIntent(R.id.noti_play_ib, musicStopPendingIntent); // ========================================================================================= // ? ? ? // ========================================================================================= Intent musicNextIntent = new Intent(getApplicationContext(), MusicService.class); musicNextIntent.setAction(ACTION_PLAY_NEXT); musicNextIntent.putExtra("position", getPositionAtPreviousOrNext(ACTION_PLAY_NEXT)); PendingIntent musicNextPendingIntent = PendingIntent.getService(getApplicationContext(), 2, musicNextIntent, PendingIntent.FLAG_UPDATE_CURRENT); remoteViews.setOnClickPendingIntent(R.id.noti_next_ib, musicNextPendingIntent); // ========================================================================================= // ? ? // ========================================================================================= Intent closeIntent = new Intent(getApplicationContext(), MusicService.class); closeIntent.setAction(ACTION_FINISH); PendingIntent closePendingIntent = PendingIntent.getService(getApplicationContext(), 3, closeIntent, PendingIntent.FLAG_UPDATE_CURRENT); remoteViews.setOnClickPendingIntent(R.id.noti_close_ib, closePendingIntent); // ========================================================================================= // Notification ? PendingIntent // ========================================================================================= Intent activityStartIntent = new Intent(MainActivity.ACTION_NAME); activityStartIntent.putExtra("restore", getCurrentInfo()); PendingIntent activityStartPendingIntent = PendingIntent.getActivity(getApplicationContext(), 1, activityStartIntent, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(activityStartPendingIntent); // ========================================================================================= Bitmap bitmap = null; if (mCurrentMusicInfo != null) { if (mCurrentMusicInfo.getUri() != null) { bitmap = MusicInfoLoadUtil.getBitmap(getApplicationContext(), mCurrentMusicInfo.getUri(), 4); } } if (bitmap == null) { bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_no_image); } remoteViews.setImageViewBitmap(R.id.noti_album_art_iv, bitmap); if (mCurrentMusicInfo != null) { remoteViews.setTextViewText(R.id.noti_artist_tv, mCurrentMusicInfo.getArtist()); remoteViews.setTextViewText(R.id.noti_title_tv, mCurrentMusicInfo.getTitle()); } builder.setContent(remoteViews); builder.setLargeIcon(bitmap); builder.setSmallIcon(R.mipmap.ic_no_image); if (mMediaPlayer.isPlaying()) { remoteViews.setImageViewResource(R.id.noti_play_ib, android.R.drawable.ic_media_pause); } else { remoteViews.setImageViewResource(R.id.noti_play_ib, android.R.drawable.ic_media_play); } builder.setAutoCancel(false); Notification notification = builder.build(); startForeground(1, notification); }