List of usage examples for android.widget RemoteViews setOnClickPendingIntent
public void setOnClickPendingIntent(int viewId, PendingIntent pendingIntent)
From source file:com.yamin.kk.service.AudioService.java
@TargetApi(Build.VERSION_CODES.JELLY_BEAN) private void showNotification() { try {//from ww w . j a v a 2 s. c om Bitmap cover = AudioUtil.getCover(this, getCurrentMedia(), 64); String title = getCurrentMedia().getTitle(); String artist = getCurrentMedia().getArtist(); String album = getCurrentMedia().getAlbum(); Notification notification; // add notification to status bar NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_stat_vlc).setTicker(title + " - " + artist).setAutoCancel(false) .setOngoing(true); Intent notificationIntent = new Intent(this, MainActivity.class); notificationIntent.setAction(MainActivity.ACTION_SHOW_PLAYER); notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER); notificationIntent.putExtra(START_FROM_NOTIFICATION, true); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); if (Util.isJellyBeanOrLater()) { Intent iBackward = new Intent(ACTION_REMOTE_BACKWARD); Intent iPlay = new Intent(ACTION_REMOTE_PLAYPAUSE); Intent iForward = new Intent(ACTION_REMOTE_FORWARD); Intent iStop = new Intent(ACTION_REMOTE_STOP); PendingIntent piBackward = PendingIntent.getBroadcast(this, 0, iBackward, PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent piPlay = PendingIntent.getBroadcast(this, 0, iPlay, PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent piForward = PendingIntent.getBroadcast(this, 0, iForward, PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent piStop = PendingIntent.getBroadcast(this, 0, iStop, PendingIntent.FLAG_UPDATE_CURRENT); RemoteViews view = new RemoteViews(getPackageName(), R.layout.notification); if (cover != null) view.setImageViewBitmap(R.id.cover, cover); view.setTextViewText(R.id.songName, title); view.setTextViewText(R.id.artist, artist); view.setImageViewResource(R.id.play_pause, mLibVLC.isPlaying() ? R.drawable.ic_pause : R.drawable.ic_play); view.setOnClickPendingIntent(R.id.play_pause, piPlay); view.setOnClickPendingIntent(R.id.forward, piForward); view.setOnClickPendingIntent(R.id.stop, piStop); view.setOnClickPendingIntent(R.id.content, pendingIntent); RemoteViews view_expanded = new RemoteViews(getPackageName(), R.layout.notification_expanded); if (cover != null) view_expanded.setImageViewBitmap(R.id.cover, cover); view_expanded.setTextViewText(R.id.songName, title); view_expanded.setTextViewText(R.id.artist, artist); view_expanded.setTextViewText(R.id.album, album); view_expanded.setImageViewResource(R.id.play_pause, mLibVLC.isPlaying() ? R.drawable.ic_pause : R.drawable.ic_play); view_expanded.setOnClickPendingIntent(R.id.backward, piBackward); view_expanded.setOnClickPendingIntent(R.id.play_pause, piPlay); view_expanded.setOnClickPendingIntent(R.id.forward, piForward); view_expanded.setOnClickPendingIntent(R.id.stop, piStop); view_expanded.setOnClickPendingIntent(R.id.content, pendingIntent); notification = builder.build(); notification.contentView = view; notification.bigContentView = view_expanded; } else { builder.setLargeIcon(cover).setContentTitle(title) .setContentText(Util.isJellyBeanOrLater() ? artist : getCurrentMedia().getSubtitle()) .setContentInfo(album).setContentIntent(pendingIntent); notification = builder.build(); } startService(new Intent(this, AudioService.class)); startForeground(3, notification); } catch (NoSuchMethodError e) { // Compat library is wrong on 3.2 // http://code.google.com/p/android/issues/detail?id=36359 // http://code.google.com/p/android/issues/detail?id=36502 } }
From source file:com.andreadec.musicplayer.MusicService.java
private void updateNotificationMessage() { /* Update remote control client */ if (Build.VERSION.SDK_INT >= 14) { if (currentPlayingItem == null) { remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_STOPPED); } else {//from www . j a v a2 s .c o m if (isPlaying()) { remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING); } else { remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PAUSED); } RemoteControlClient.MetadataEditor metadataEditor = remoteControlClient.editMetadata(true); metadataEditor.putString(MediaMetadataRetriever.METADATA_KEY_TITLE, currentPlayingItem.getTitle()); metadataEditor.putString(MediaMetadataRetriever.METADATA_KEY_ALBUM, currentPlayingItem.getArtist()); metadataEditor.putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, currentPlayingItem.getArtist()); metadataEditor.putLong(MediaMetadataRetriever.METADATA_KEY_DURATION, getDuration()); if (currentPlayingItem.hasImage()) { metadataEditor.putBitmap(METADATA_KEY_ARTWORK, currentPlayingItem.getImage()); } else { metadataEditor.putBitmap(METADATA_KEY_ARTWORK, icon.copy(icon.getConfig(), false)); } metadataEditor.apply(); } } /* Update notification */ NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this); notificationBuilder.setSmallIcon(R.drawable.audio_white); //notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)); notificationBuilder.setOngoing(true); int playPauseIcon = isPlaying() ? R.drawable.pause : R.drawable.play; //int playPauseString = isPlaying() ? R.string.pause : R.string.play; notificationBuilder.setContentIntent(pendingIntent); String notificationMessage = ""; if (currentPlayingItem == null) { notificationMessage = getResources().getString(R.string.noSong); } else { if (currentPlayingItem.getArtist() != null && !currentPlayingItem.getArtist().equals("")) notificationMessage = currentPlayingItem.getArtist() + " - "; notificationMessage += currentPlayingItem.getTitle(); } /*notificationBuilder.addAction(R.drawable.previous, getResources().getString(R.string.previous), previousPendingIntent); notificationBuilder.addAction(playPauseIcon, getResources().getString(playPauseString), playpausePendingIntent); notificationBuilder.addAction(R.drawable.next, getResources().getString(R.string.next), nextPendingIntent);*/ notificationBuilder.setContentTitle(getResources().getString(R.string.app_name)); notificationBuilder.setContentText(notificationMessage); notification = notificationBuilder.build(); RemoteViews notificationLayout = new RemoteViews(getPackageName(), R.layout.layout_notification); if (currentPlayingItem == null) { notificationLayout.setTextViewText(R.id.textViewArtist, getString(R.string.app_name)); notificationLayout.setTextViewText(R.id.textViewTitle, getString(R.string.noSong)); notificationLayout.setImageViewBitmap(R.id.imageViewNotification, BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)); } else { notificationLayout.setTextViewText(R.id.textViewArtist, currentPlayingItem.getArtist()); notificationLayout.setTextViewText(R.id.textViewTitle, currentPlayingItem.getTitle()); Bitmap image = currentPlayingItem.getImage(); if (image != null) { notificationLayout.setImageViewBitmap(R.id.imageViewNotification, image); } else { notificationLayout.setImageViewBitmap(R.id.imageViewNotification, BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)); } } notificationLayout.setOnClickPendingIntent(R.id.buttonNotificationQuit, quitPendingIntent); notificationLayout.setOnClickPendingIntent(R.id.buttonNotificationPrevious, previousPendingIntent); notificationLayout.setImageViewResource(R.id.buttonNotificationPlayPause, playPauseIcon); notificationLayout.setOnClickPendingIntent(R.id.buttonNotificationPlayPause, playpausePendingIntent); notificationLayout.setOnClickPendingIntent(R.id.buttonNotificationNext, nextPendingIntent); notification.bigContentView = notificationLayout; notificationManager.notify(Constants.NOTIFICATION_MAIN, notification); }
From source file:org.physical_web.physicalweb.UriBeaconDiscoveryService.java
private void updateSummaryNotificationRemoteViewsFirstBeacon(String url, RemoteViews remoteViews) { MetadataResolver.UrlMetadata urlMetadata_firstBeacon = mUrlToUrlMetadata.get(url); if (urlMetadata_firstBeacon != null) { String title = mUrlToUrlMetadata.get(url).title; String description = mUrlToUrlMetadata.get(url).description; Bitmap icon = mUrlToUrlMetadata.get(url).icon; remoteViews.setImageViewBitmap(R.id.icon_firstBeacon, icon); remoteViews.setTextViewText(R.id.title_firstBeacon, title); remoteViews.setTextViewText(R.id.url_firstBeacon, url); remoteViews.setTextViewText(R.id.description_firstBeacon, description); // Recolor notifications to have light text for non-Lollipop devices if (!(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)) { remoteViews.setTextColor(R.id.title_firstBeacon, NON_LOLLIPOP_NOTIFICATION_TITLE_COLOR); remoteViews.setTextColor(R.id.url_firstBeacon, NON_LOLLIPOP_NOTIFICATION_URL_COLOR); remoteViews.setTextColor(R.id.description_firstBeacon, NON_LOLLIPOP_NOTIFICATION_SNIPPET_COLOR); }// w ww .j a va 2 s . c om // Create an intent that will open the browser to the beacon's url // if the user taps the notification PendingIntent pendingIntent = createNavigateToUrlPendingIntent(url); remoteViews.setOnClickPendingIntent(R.id.first_beacon_main_layout, pendingIntent); remoteViews.setViewVisibility(R.id.firstBeaconLayout, View.VISIBLE); } else { remoteViews.setViewVisibility(R.id.firstBeaconLayout, View.GONE); } }
From source file:org.physical_web.physicalweb.UriBeaconDiscoveryService.java
private void updateSummaryNotificationRemoteViewsSecondBeacon(String url, RemoteViews remoteViews) { MetadataResolver.UrlMetadata urlMetadata_secondBeacon = mUrlToUrlMetadata.get(url); if (urlMetadata_secondBeacon != null) { String title = mUrlToUrlMetadata.get(url).title; String description = mUrlToUrlMetadata.get(url).description; Bitmap icon = mUrlToUrlMetadata.get(url).icon; remoteViews.setImageViewBitmap(R.id.icon_secondBeacon, icon); remoteViews.setTextViewText(R.id.title_secondBeacon, title); remoteViews.setTextViewText(R.id.url_secondBeacon, url); remoteViews.setTextViewText(R.id.description_secondBeacon, description); // Recolor notifications to have light text for non-Lollipop devices if (!(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)) { remoteViews.setTextColor(R.id.title_secondBeacon, NON_LOLLIPOP_NOTIFICATION_TITLE_COLOR); remoteViews.setTextColor(R.id.url_secondBeacon, NON_LOLLIPOP_NOTIFICATION_URL_COLOR); remoteViews.setTextColor(R.id.description_secondBeacon, NON_LOLLIPOP_NOTIFICATION_SNIPPET_COLOR); }/*from w w w . j a va 2s.c o m*/ // Create an intent that will open the browser to the beacon's url // if the user taps the notification PendingIntent pendingIntent = createNavigateToUrlPendingIntent(url); remoteViews.setOnClickPendingIntent(R.id.second_beacon_main_layout, pendingIntent); remoteViews.setViewVisibility(R.id.secondBeaconLayout, View.VISIBLE); } else { remoteViews.setViewVisibility(R.id.secondBeaconLayout, View.GONE); } }
From source file:org.videolan.vlc.audio.AudioService.java
@TargetApi(Build.VERSION_CODES.JELLY_BEAN) private void showNotification() { try {//from w w w . ja v a 2 s . c o m Media media = getCurrentMedia(); if (media == null) return; Bitmap cover = AudioUtil.getCover(this, media, 64); String title = media.getTitle(); String artist = media.getArtist(); String album = media.getAlbum(); Notification notification; // add notification to status bar NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_stat_vlc).setTicker(title + " - " + artist).setAutoCancel(false) .setOngoing(true); Intent notificationIntent = new Intent(this, MainActivity.class); notificationIntent.setAction(MainActivity.ACTION_SHOW_PLAYER); notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER); notificationIntent.putExtra(START_FROM_NOTIFICATION, true); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); if (LibVlcUtil.isJellyBeanOrLater()) { Intent iBackward = new Intent(ACTION_REMOTE_BACKWARD); Intent iPlay = new Intent(ACTION_REMOTE_PLAYPAUSE); Intent iForward = new Intent(ACTION_REMOTE_FORWARD); Intent iStop = new Intent(ACTION_REMOTE_STOP); PendingIntent piBackward = PendingIntent.getBroadcast(this, 0, iBackward, PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent piPlay = PendingIntent.getBroadcast(this, 0, iPlay, PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent piForward = PendingIntent.getBroadcast(this, 0, iForward, PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent piStop = PendingIntent.getBroadcast(this, 0, iStop, PendingIntent.FLAG_UPDATE_CURRENT); RemoteViews view = new RemoteViews(getPackageName(), R.layout.notification); if (cover != null) view.setImageViewBitmap(R.id.cover, cover); view.setTextViewText(R.id.songName, title); view.setTextViewText(R.id.artist, artist); view.setImageViewResource(R.id.play_pause, mLibVLC.isPlaying() ? R.drawable.ic_pause_w : R.drawable.ic_play_w); view.setOnClickPendingIntent(R.id.play_pause, piPlay); view.setOnClickPendingIntent(R.id.forward, piForward); view.setOnClickPendingIntent(R.id.stop, piStop); view.setOnClickPendingIntent(R.id.content, pendingIntent); RemoteViews view_expanded = new RemoteViews(getPackageName(), R.layout.notification_expanded); if (cover != null) view_expanded.setImageViewBitmap(R.id.cover, cover); view_expanded.setTextViewText(R.id.songName, title); view_expanded.setTextViewText(R.id.artist, artist); view_expanded.setTextViewText(R.id.album, album); view_expanded.setImageViewResource(R.id.play_pause, mLibVLC.isPlaying() ? R.drawable.ic_pause_w : R.drawable.ic_play_w); view_expanded.setOnClickPendingIntent(R.id.backward, piBackward); view_expanded.setOnClickPendingIntent(R.id.play_pause, piPlay); view_expanded.setOnClickPendingIntent(R.id.forward, piForward); view_expanded.setOnClickPendingIntent(R.id.stop, piStop); view_expanded.setOnClickPendingIntent(R.id.content, pendingIntent); notification = builder.build(); notification.contentView = view; notification.bigContentView = view_expanded; } else { builder.setLargeIcon(cover).setContentTitle(title) .setContentText(LibVlcUtil.isJellyBeanOrLater() ? artist : media.getSubtitle()) .setContentInfo(album).setContentIntent(pendingIntent); notification = builder.build(); } startService(new Intent(this, AudioService.class)); startForeground(3, notification); } catch (NoSuchMethodError e) { // Compat library is wrong on 3.2 // http://code.google.com/p/android/issues/detail?id=36359 // http://code.google.com/p/android/issues/detail?id=36502 } }
From source file:com.freeme.filemanager.FileExplorerTabActivity.java
public void showButtonNotify() { mBuilder = new Builder(this); RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.view_custom_button); if (IsFreemeOs.isFreemeOs()) { Bitmap bitMap = getApplicationContext().getPackageManager().getIconBitmapWithThemeBg( getResources().getDrawable(R.drawable.notification_clean), getApplicationContext().getPackageName(), 0); remoteViews.setImageViewBitmap(R.id.filemanager_notification_clean, bitMap); } else {//w w w.j av a2s . c om remoteViews.setImageViewResource(R.id.filemanager_notification_clean, R.drawable.notification_clean); } remoteViews.setTextViewText(R.id.tv_custom_title, getString(R.string.fileManager)); remoteViews.setTextViewText(R.id.tv_custom_text, getString(R.string.clean_text)); Intent buttonIntent = new Intent(ACTION_BUTTON); buttonIntent.putExtra(INTENT_BUTTONID_TAG, BUTTON_CLEAN_ID); PendingIntent intent_clean = PendingIntent.getBroadcast(this, 1, buttonIntent, PendingIntent.FLAG_UPDATE_CURRENT); remoteViews.setOnClickPendingIntent(R.id.btn_custom, intent_clean); mBuilder.setContent(remoteViews).setContentIntent(getDefalutIntent(Notification.FLAG_AUTO_CANCEL)) .setWhen(System.currentTimeMillis()).setPriority(Notification.PRIORITY_DEFAULT).setOngoing(true) .setSmallIcon(R.drawable.notification_small_clean); notify = mBuilder.build(); notify.flags = Notification.FLAG_AUTO_CANCEL; mNotificationManager.notify(200, notify); }
From source file:uk.org.ngo.squeezer.service.SqueezeService.java
/** * Manages the state of any ongoing notification based on the player and connection state. *//*from w ww . j a v a2s. c o m*/ private void updateOngoingNotification() { Player activePlayer = this.mActivePlayer.get(); PlayerState activePlayerState = getActivePlayerState(); // Update scrobble state, if either we're currently scrobbling, or we // were (to catch the case where we started scrobbling a song, and the // user went in to settings to disable scrobbling). if (scrobblingEnabled || scrobblingPreviouslyEnabled) { scrobblingPreviouslyEnabled = scrobblingEnabled; Scrobble.scrobbleFromPlayerState(this, activePlayerState); } // If there's no active player then kill the notification and get out. // TODO: Have a "There are no connected players" notification text. if (activePlayer == null || activePlayerState == null) { clearOngoingNotification(); return; } boolean playing = activePlayerState.isPlaying(); // If the song is not playing and the user wants notifications only when playing then // kill the notification and get out. if (!playing && !mShowNotificationWhenNotPlaying) { clearOngoingNotification(); return; } // If there's no current song then kill the notification and get out. // TODO: Have a "There's nothing playing" notification text. final Song currentSong = activePlayerState.getCurrentSong(); if (currentSong == null) { clearOngoingNotification(); return; } // Compare the current state with the state when the notification was last updated. // If there are no changes (same song, same playing state) then there's nothing to do. String songName = currentSong.getName(); String albumName = currentSong.getAlbumName(); String artistName = currentSong.getArtist(); Uri url = currentSong.getArtworkUrl(); String playerName = activePlayer.getName(); if (mNotifiedPlayerState == null) { mNotifiedPlayerState = new PlayerState(); } else { boolean lastPlaying = mNotifiedPlayerState.isPlaying(); Song lastNotifiedSong = mNotifiedPlayerState.getCurrentSong(); // No change in state if (playing == lastPlaying && currentSong.equals(lastNotifiedSong)) { return; } } mNotifiedPlayerState.setCurrentSong(currentSong); mNotifiedPlayerState.setPlayStatus(activePlayerState.getPlayStatus()); final NotificationManagerCompat nm = NotificationManagerCompat.from(this); PendingIntent nextPendingIntent = getPendingIntent(ACTION_NEXT_TRACK); PendingIntent prevPendingIntent = getPendingIntent(ACTION_PREV_TRACK); PendingIntent playPendingIntent = getPendingIntent(ACTION_PLAY); PendingIntent pausePendingIntent = getPendingIntent(ACTION_PAUSE); PendingIntent closePendingIntent = getPendingIntent(ACTION_CLOSE); Intent showNowPlaying = new Intent(this, NowPlayingActivity.class) .setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); PendingIntent pIntent = PendingIntent.getActivity(this, 0, showNowPlaying, 0); Notification notification; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { final Notification.Builder builder = new Notification.Builder(this); builder.setContentIntent(pIntent); builder.setSmallIcon(R.drawable.squeezer_notification); builder.setVisibility(Notification.VISIBILITY_PUBLIC); builder.setShowWhen(false); builder.setContentTitle(songName); builder.setContentText(albumName); builder.setSubText(playerName); builder.setStyle(new Notification.MediaStyle().setShowActionsInCompactView(1, 2) .setMediaSession(mMediaSession.getSessionToken())); final MediaMetadata.Builder metaBuilder = new MediaMetadata.Builder(); metaBuilder.putString(MediaMetadata.METADATA_KEY_ARTIST, artistName); metaBuilder.putString(MediaMetadata.METADATA_KEY_ALBUM, albumName); metaBuilder.putString(MediaMetadata.METADATA_KEY_TITLE, songName); mMediaSession.setMetadata(metaBuilder.build()); // Don't set an ongoing notification, otherwise wearable's won't show it. builder.setOngoing(false); builder.setDeleteIntent(closePendingIntent); if (playing) { builder.addAction( new Notification.Action(R.drawable.ic_action_previous, "Previous", prevPendingIntent)) .addAction(new Notification.Action(R.drawable.ic_action_pause, "Pause", pausePendingIntent)) .addAction(new Notification.Action(R.drawable.ic_action_next, "Next", nextPendingIntent)); } else { builder.addAction( new Notification.Action(R.drawable.ic_action_previous, "Previous", prevPendingIntent)) .addAction(new Notification.Action(R.drawable.ic_action_play, "Play", playPendingIntent)) .addAction(new Notification.Action(R.drawable.ic_action_next, "Next", nextPendingIntent)); } ImageFetcher.getInstance(this).loadImage(url, getResources().getDimensionPixelSize(android.R.dimen.notification_large_icon_width), getResources().getDimensionPixelSize(android.R.dimen.notification_large_icon_height), new ImageWorker.ImageWorkerCallback() { @Override @TargetApi(Build.VERSION_CODES.LOLLIPOP) public void process(Object data, @Nullable Bitmap bitmap) { if (bitmap == null) { bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon_album_noart); } metaBuilder.putBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART, bitmap); metaBuilder.putBitmap(MediaMetadata.METADATA_KEY_ART, bitmap); mMediaSession.setMetadata(metaBuilder.build()); builder.setLargeIcon(bitmap); nm.notify(PLAYBACKSERVICE_STATUS, builder.build()); } }); } else { NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setOngoing(true); builder.setCategory(NotificationCompat.CATEGORY_SERVICE); builder.setSmallIcon(R.drawable.squeezer_notification); RemoteViews normalView = new RemoteViews(this.getPackageName(), R.layout.notification_player_normal); RemoteViews expandedView = new RemoteViews(this.getPackageName(), R.layout.notification_player_expanded); normalView.setOnClickPendingIntent(R.id.next, nextPendingIntent); expandedView.setOnClickPendingIntent(R.id.previous, prevPendingIntent); expandedView.setOnClickPendingIntent(R.id.next, nextPendingIntent); builder.setContent(normalView); normalView.setTextViewText(R.id.trackname, songName); normalView.setTextViewText(R.id.albumname, albumName); expandedView.setTextViewText(R.id.trackname, songName); expandedView.setTextViewText(R.id.albumname, albumName); expandedView.setTextViewText(R.id.player_name, playerName); if (playing) { normalView.setImageViewResource(R.id.pause, R.drawable.ic_action_pause); normalView.setOnClickPendingIntent(R.id.pause, pausePendingIntent); expandedView.setImageViewResource(R.id.pause, R.drawable.ic_action_pause); expandedView.setOnClickPendingIntent(R.id.pause, pausePendingIntent); } else { normalView.setImageViewResource(R.id.pause, R.drawable.ic_action_play); normalView.setOnClickPendingIntent(R.id.pause, playPendingIntent); expandedView.setImageViewResource(R.id.pause, R.drawable.ic_action_play); expandedView.setOnClickPendingIntent(R.id.pause, playPendingIntent); } builder.setContentTitle(songName); builder.setContentText(getString(R.string.notification_playing_text, playerName)); builder.setContentIntent(pIntent); notification = builder.build(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { notification.bigContentView = expandedView; } nm.notify(PLAYBACKSERVICE_STATUS, notification); ImageFetcher.getInstance(this).loadImage(this, url, normalView, R.id.album, getResources().getDimensionPixelSize(R.dimen.album_art_icon_normal_notification_width), getResources().getDimensionPixelSize(R.dimen.album_art_icon_normal_notification_height), nm, PLAYBACKSERVICE_STATUS, notification); ImageFetcher.getInstance(this).loadImage(this, url, expandedView, R.id.album, getResources().getDimensionPixelSize(R.dimen.album_art_icon_expanded_notification_width), getResources().getDimensionPixelSize(R.dimen.album_art_icon_expanded_notification_height), nm, PLAYBACKSERVICE_STATUS, notification); } }
From source file:com.dzt.musicplay.player.AudioService.java
@TargetApi(Build.VERSION_CODES.JELLY_BEAN) private void showNotification() { Constant.print_i("AudioService----------->showNotification"); try {//from w w w . j a va 2 s . co m Media media = getCurrentMedia(); if (media == null) return; Bitmap cover = AudioUtil.getCover(this, media, 64); String title = media.getTitle(); String artist = media.getArtist(); String album = media.getAlbum(); Notification notification; // add notification to status bar NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.icon_music).setTicker(title + " - " + artist).setAutoCancel(false) .setOngoing(true); //Activity Intent notificationIntent = new Intent(this, MusicPlayActivity.class); notificationIntent.setAction(MusicPlayActivity.ACTION_SHOW_PLAYER); notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER); notificationIntent.putExtra(START_FROM_NOTIFICATION, true); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); if (LibVlcUtil.isJellyBeanOrLater()) { Intent iBackward = new Intent(ACTION_REMOTE_BACKWARD); Intent iPlay = new Intent(ACTION_REMOTE_PLAYPAUSE); Intent iForward = new Intent(ACTION_REMOTE_FORWARD); Intent iStop = new Intent(ACTION_REMOTE_STOP); PendingIntent piBackward = PendingIntent.getBroadcast(this, 0, iBackward, PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent piPlay = PendingIntent.getBroadcast(this, 0, iPlay, PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent piForward = PendingIntent.getBroadcast(this, 0, iForward, PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent piStop = PendingIntent.getBroadcast(this, 0, iStop, PendingIntent.FLAG_UPDATE_CURRENT); RemoteViews view = new RemoteViews(getPackageName(), R.layout.notification); if (cover != null) view.setImageViewBitmap(R.id.cover, cover); view.setTextViewText(R.id.songName, title); view.setTextViewText(R.id.artist, artist); view.setImageViewResource(R.id.play_pause, mLibVLC.isPlaying() ? R.drawable.ic_pause_w : R.drawable.ic_play_w); view.setOnClickPendingIntent(R.id.play_pause, piPlay); view.setOnClickPendingIntent(R.id.forward, piForward); view.setOnClickPendingIntent(R.id.stop, piStop); view.setOnClickPendingIntent(R.id.content, pendingIntent); RemoteViews view_expanded = new RemoteViews(getPackageName(), R.layout.notification_expanded); if (cover != null) view_expanded.setImageViewBitmap(R.id.cover, cover); view_expanded.setTextViewText(R.id.songName, title); view_expanded.setTextViewText(R.id.artist, artist); view_expanded.setTextViewText(R.id.album, album); view_expanded.setImageViewResource(R.id.play_pause, mLibVLC.isPlaying() ? R.drawable.ic_pause_w : R.drawable.ic_play_w); view_expanded.setOnClickPendingIntent(R.id.backward, piBackward); view_expanded.setOnClickPendingIntent(R.id.play_pause, piPlay); view_expanded.setOnClickPendingIntent(R.id.forward, piForward); view_expanded.setOnClickPendingIntent(R.id.stop, piStop); view_expanded.setOnClickPendingIntent(R.id.content, pendingIntent); notification = builder.build(); notification.contentView = view; notification.bigContentView = view_expanded; } else { builder.setLargeIcon(cover).setContentTitle(title) .setContentText(LibVlcUtil.isJellyBeanOrLater() ? artist : media.getSubtitle()) .setContentInfo(album).setContentIntent(pendingIntent); notification = builder.build(); } startService(new Intent(this, AudioService.class)); startForeground(3, notification); } catch (NoSuchMethodError e) { // Compat library is wrong on 3.2 // http://code.google.com/p/android/issues/detail?id=36359 // http://code.google.com/p/android/issues/detail?id=36502 } Constant.print_i("AudioService----------->showNotification----end"); }
From source file:com.kasungunathilaka.sarigama.service.PlayerService.java
private void setupNotification(Song song, String notificationType) { Bitmap remote_picture = null;/* w ww .j ava 2 s . co m*/ RemoteViews smallNotification; RemoteViews bigNotification; Notification notification; NotificationManager notificationManager = (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE); if (song != null) { if (song.getSongImage().contentEquals("http://sarigama.lk/img/songs/default.jpg")) { remote_picture = BitmapFactory.decodeResource(PlayerService.this.getResources(), R.drawable.default_song_art); } else { try { remote_picture = BitmapFactory .decodeStream((InputStream) new URL(song.getSongImage()).getContent()); } catch (Exception e) { remote_picture = BitmapFactory.decodeResource(PlayerService.this.getResources(), R.drawable.default_song_art); e.printStackTrace(); } } } switch (notificationType) { case HomeActivity.NOTIFICATION_TYPE_PLAY: try { smallNotification = new RemoteViews(getPackageName(), R.layout.notification_layout_small); smallNotification.setImageViewBitmap(R.id.ivSmallNotificationSongImage, remote_picture); smallNotification.setTextViewText(R.id.tvSmallNotificationSong, song.getSongTitle()); smallNotification.setTextViewText(R.id.tvSmallNotificationArtist, song.getSongArtist()); smallNotification.setImageViewResource(R.id.ibSmallPlay, R.drawable.ic_pause_white); smallNotification.setOnClickPendingIntent(R.id.flSmallPlay, playIntent); smallNotification.setOnClickPendingIntent(R.id.ivSmallNext, nextIntent); bigNotification = new RemoteViews(getPackageName(), R.layout.notification_layout_big); bigNotification.setImageViewBitmap(R.id.ivBigNotificationSongImage, remote_picture); bigNotification.setTextViewText(R.id.tvBigNotificationSong, song.getSongTitle()); bigNotification.setTextViewText(R.id.tvBigNotificationArtist, song.getSongArtist()); bigNotification.setImageViewResource(R.id.ibBigPlay, R.drawable.ic_pause_white); bigNotification.setOnClickPendingIntent(R.id.ibBigNext, nextIntent); bigNotification.setOnClickPendingIntent(R.id.ibBigPlay, playIntent); bigNotification.setOnClickPendingIntent(R.id.ibBigPrevious, previousIntent); notification = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_play_white) .setAutoCancel(false).setOngoing(true).setContent(smallNotification) .setContentIntent(startIntent).build(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { notification.bigContentView = bigNotification; } notificationManager.notify(NOTIFICATION_ID, notification); } catch (Exception e) { e.printStackTrace(); } break; case HomeActivity.NOTIFICATION_TYPE_PAUSE: try { smallNotification = new RemoteViews(getPackageName(), R.layout.notification_layout_small); smallNotification.setImageViewResource(R.id.ibSmallPlay, R.drawable.ic_play_white); bigNotification = new RemoteViews(getPackageName(), R.layout.notification_layout_big); bigNotification.setImageViewBitmap(R.id.ivBigNotificationSongImage, remote_picture); bigNotification.setTextViewText(R.id.tvBigNotificationSong, song.getSongTitle()); bigNotification.setTextViewText(R.id.tvBigNotificationArtist, song.getSongArtist()); bigNotification.setImageViewResource(R.id.ibBigPlay, R.drawable.ic_play_white); bigNotification.setOnClickPendingIntent(R.id.ibBigNext, nextIntent); bigNotification.setOnClickPendingIntent(R.id.ibBigPlay, playIntent); bigNotification.setOnClickPendingIntent(R.id.ibBigPrevious, previousIntent); notification = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_pause_white) .setOngoing(false).setAutoCancel(false).setContent(smallNotification) .setContentIntent(startIntent).build(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { notification.bigContentView = bigNotification; } notificationManager.notify(NOTIFICATION_ID, notification); } catch (Exception e) { e.printStackTrace(); } break; case HomeActivity.NOTIFICATION_TYPE_DISMISS: notificationManager.cancel(NOTIFICATION_ID); break; } }
From source file:org.videolan.vlc.PlaybackService.java
@TargetApi(Build.VERSION_CODES.LOLLIPOP) private void showNotification() { try {/*from www .j a v a 2 s .co m*/ MediaWrapper media = getCurrentMedia(); if (media == null) return; Bitmap cover = AudioUtil.getCover(this, media, 64); String title = media.getTitle(); String artist = Util.getMediaArtist(this, media); String album = Util.getMediaAlbum(this, media); Notification notification; if (media.isArtistUnknown() && media.isAlbumUnknown() && media.getNowPlaying() != null) { artist = media.getNowPlaying(); album = ""; } //Watch notification dismissed PendingIntent piStop = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_REMOTE_STOP), PendingIntent.FLAG_UPDATE_CURRENT); // add notification to status bar NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_stat_vlc).setTicker(title + " - " + artist) .setAutoCancel(!MediaPlayer().isPlaying()).setOngoing(MediaPlayer().isPlaying()) .setDeleteIntent(piStop); Intent notificationIntent = new Intent(this, MainActivity.class); notificationIntent.setAction(AudioPlayerContainerActivity.ACTION_SHOW_PLAYER); notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER); notificationIntent.putExtra(START_FROM_NOTIFICATION, true); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); if (AndroidUtil.isJellyBeanOrLater()) { Intent iBackward = new Intent(ACTION_REMOTE_BACKWARD); Intent iPlay = new Intent(ACTION_REMOTE_PLAYPAUSE); Intent iForward = new Intent(ACTION_REMOTE_FORWARD); PendingIntent piBackward = PendingIntent.getBroadcast(this, 0, iBackward, PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent piPlay = PendingIntent.getBroadcast(this, 0, iPlay, PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent piForward = PendingIntent.getBroadcast(this, 0, iForward, PendingIntent.FLAG_UPDATE_CURRENT); RemoteViews view = new RemoteViews(BuildConfig.APPLICATION_ID, R.layout.notification); view.setImageViewBitmap(R.id.cover, cover == null ? BitmapFactory.decodeResource(getResources(), R.drawable.icon) : cover); view.setTextViewText(R.id.songName, title); view.setTextViewText(R.id.artist, artist); view.setImageViewResource(R.id.play_pause, MediaPlayer().isPlaying() ? R.drawable.ic_pause_w : R.drawable.ic_play_w); view.setOnClickPendingIntent(R.id.play_pause, piPlay); view.setOnClickPendingIntent(R.id.forward, piForward); view.setOnClickPendingIntent(R.id.stop, piStop); view.setOnClickPendingIntent(R.id.content, pendingIntent); RemoteViews view_expanded = new RemoteViews(BuildConfig.APPLICATION_ID, R.layout.notification_expanded); view_expanded.setImageViewBitmap(R.id.cover, cover == null ? BitmapFactory.decodeResource(getResources(), R.drawable.icon) : cover); view_expanded.setTextViewText(R.id.songName, title); view_expanded.setTextViewText(R.id.artist, artist); view_expanded.setTextViewText(R.id.album, album); view_expanded.setImageViewResource(R.id.play_pause, MediaPlayer().isPlaying() ? R.drawable.ic_pause_w : R.drawable.ic_play_w); view_expanded.setOnClickPendingIntent(R.id.backward, piBackward); view_expanded.setOnClickPendingIntent(R.id.play_pause, piPlay); view_expanded.setOnClickPendingIntent(R.id.forward, piForward); view_expanded.setOnClickPendingIntent(R.id.stop, piStop); view_expanded.setOnClickPendingIntent(R.id.content, pendingIntent); if (AndroidUtil.isLolliPopOrLater()) { //Hide stop button on pause, we swipe notification to stop view.setViewVisibility(R.id.stop, MediaPlayer().isPlaying() ? View.VISIBLE : View.INVISIBLE); view_expanded.setViewVisibility(R.id.stop, MediaPlayer().isPlaying() ? View.VISIBLE : View.INVISIBLE); //Make notification appear on lockscreen builder.setVisibility(Notification.VISIBILITY_PUBLIC); } notification = builder.build(); notification.contentView = view; notification.bigContentView = view_expanded; } else { builder.setLargeIcon( cover == null ? BitmapFactory.decodeResource(getResources(), R.drawable.icon) : cover) .setContentTitle(title) .setContentText( AndroidUtil.isJellyBeanOrLater() ? artist : Util.getMediaSubtitle(this, media)) .setContentInfo(album).setContentIntent(pendingIntent); notification = builder.build(); } startService(new Intent(this, PlaybackService.class)); if (!AndroidUtil.isLolliPopOrLater() || MediaPlayer().isPlaying()) startForeground(3, notification); else { stopForeground(false); NotificationManagerCompat.from(this).notify(3, notification); } } catch (NoSuchMethodError e) { // Compat library is wrong on 3.2 // http://code.google.com/p/android/issues/detail?id=36359 // http://code.google.com/p/android/issues/detail?id=36502 } }