如何使用支持库 v25 显示音乐播放器通知
How to display music player notification using support libs v25
我使用以下代码来显示自定义通知,但它没有显示。
以下摘自我的服务class。点击歌曲播放成功,除了提示外一切正常
编译 sdk 版本 25 -
最低 SDK 21 -
目标 SDK 25
private void updateNotification ( ) {
// try {
// normal notif
RemoteViews smallView = new RemoteViews ( getPackageName ( ), R.layout.notification );
smallView.setTextViewText ( R.id.notification_song_name, currentSong.getTitle ( ) );
smallView.setTextViewText ( R.id.notification_artist_name, currentSong.getArtist ( ) );
// expanded notif
RemoteViews expanedView = new RemoteViews ( getPackageName ( ), R.layout.notification_expanded );
expanedView.setTextViewText ( R.id.notification_song_name, currentSong.getTitle ( ) );
expanedView.setTextViewText ( R.id.notification_album_name, currentSong.getAlbum ( ) );
expanedView.setTextViewText ( R.id.notification_artist_name, currentSong.getArtist ( ) );
if ( PlayerConstants.SONG_PAUSED ) {
smallView.setImageViewBitmap ( R.id.notification_play_pause_button, ( (BitmapDrawable) getResources ( ).getDrawable ( R.mipmap.ic_play_arrow_black_24dp ) ).getBitmap ( ) );
expanedView.setImageViewBitmap ( R.id.notification_play_pause_button, ( (BitmapDrawable) getResources ( ).getDrawable ( R.mipmap.ic_play_arrow_black_24dp ) ).getBitmap ( ) );
}
else {
smallView.setImageViewBitmap ( R.id.notification_play_pause_button, ( (BitmapDrawable) getResources ( ).getDrawable ( R.mipmap.ic_pause_black_24dp ) ).getBitmap ( ) );
expanedView.setImageViewBitmap ( R.id.notification_play_pause_button, ( (BitmapDrawable) getResources ( ).getDrawable ( R.mipmap.ic_pause_black_24dp ) ).getBitmap ( ) );
}
if ( currentSong.getIsLiked ( this ) )
expanedView.setImageViewBitmap ( R.id.notification_favorite_button, ( (BitmapDrawable) getResources ( ).getDrawable ( R.drawable.ic_favorite_red_24dp ) ).getBitmap ( ) );
else
expanedView.setImageViewBitmap ( R.id.notification_favorite_button, ( (BitmapDrawable) getResources ( ).getDrawable ( R.mipmap.ic_favorite_border_black_24dp ) ).getBitmap ( ) );
setListeners ( smallView );
setListeners ( expanedView );
Bitmap albumArt = AudioExtensionMethods.getBitMap ( getBaseContext ( ), currentSong.getAlbumArtLocation ( ) );
if ( albumArt != null ) {
smallView.setImageViewBitmap ( R.id.notification_album_art, albumArt );
expanedView.setImageViewBitmap ( R.id.notification_album_art, albumArt );
}
else {
smallView.setImageViewBitmap ( R.id.notification_album_art, ( (BitmapDrawable) getResources ( ).getDrawable ( R.mipmap.unkown_album_art ) ).getBitmap ( ) );
expanedView.setImageViewBitmap ( R.id.notification_album_art, ( (BitmapDrawable) getResources ( ).getDrawable ( R.mipmap.unkown_album_art ) ).getBitmap ( ) );
}
// create an ongoing notif
NotificationCompat.Builder builder = new NotificationCompat.Builder ( this )
.setContent ( smallView )
.setSmallIcon ( R.mipmap.launcher_icon )
.setOngoing ( true );
// makes the notif dismissable when playback is paused
if ( PlayerConstants.SONG_PAUSED ) {
builder.setOngoing ( false );
}
if ( currentVersionSupportBigNotification ) {
builder.setCustomBigContentView ( expanedView );
}
Intent nIntent = new Intent ( this, MainActivity.class );
nIntent.putExtra ( "notificationIntent", true );
nIntent.addFlags ( Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP );
PendingIntent pendingIntent = PendingIntent.getActivity ( this, 0, nIntent, PendingIntent.FLAG_UPDATE_CURRENT );
builder.setContentIntent ( pendingIntent );
// send the notification
Notification build = builder.build ( );
notificationManager = (NotificationManager) getSystemService ( NOTIFICATION_SERVICE );
notificationManager.notify ( NOTIFICATION_ID, build );
// }
// catch (Exception ignored) {}
}
我不知道这里出了什么问题。任何帮助,将不胜感激。
P.S。我不愿意更新我的库,因为更新会使应用程序崩溃,而且我无法从头开始编写整个代码。
哇。有趣的是我是如何解决它的。事实证明,通知和小部件不支持主题或任何你称之为的主题。我在通知的布局中使用 "attr/primaryColor",将它们更改为正常颜色格式解决了问题。
我使用以下代码来显示自定义通知,但它没有显示。 以下摘自我的服务class。点击歌曲播放成功,除了提示外一切正常
编译 sdk 版本 25 - 最低 SDK 21 - 目标 SDK 25
private void updateNotification ( ) {
// try {
// normal notif
RemoteViews smallView = new RemoteViews ( getPackageName ( ), R.layout.notification );
smallView.setTextViewText ( R.id.notification_song_name, currentSong.getTitle ( ) );
smallView.setTextViewText ( R.id.notification_artist_name, currentSong.getArtist ( ) );
// expanded notif
RemoteViews expanedView = new RemoteViews ( getPackageName ( ), R.layout.notification_expanded );
expanedView.setTextViewText ( R.id.notification_song_name, currentSong.getTitle ( ) );
expanedView.setTextViewText ( R.id.notification_album_name, currentSong.getAlbum ( ) );
expanedView.setTextViewText ( R.id.notification_artist_name, currentSong.getArtist ( ) );
if ( PlayerConstants.SONG_PAUSED ) {
smallView.setImageViewBitmap ( R.id.notification_play_pause_button, ( (BitmapDrawable) getResources ( ).getDrawable ( R.mipmap.ic_play_arrow_black_24dp ) ).getBitmap ( ) );
expanedView.setImageViewBitmap ( R.id.notification_play_pause_button, ( (BitmapDrawable) getResources ( ).getDrawable ( R.mipmap.ic_play_arrow_black_24dp ) ).getBitmap ( ) );
}
else {
smallView.setImageViewBitmap ( R.id.notification_play_pause_button, ( (BitmapDrawable) getResources ( ).getDrawable ( R.mipmap.ic_pause_black_24dp ) ).getBitmap ( ) );
expanedView.setImageViewBitmap ( R.id.notification_play_pause_button, ( (BitmapDrawable) getResources ( ).getDrawable ( R.mipmap.ic_pause_black_24dp ) ).getBitmap ( ) );
}
if ( currentSong.getIsLiked ( this ) )
expanedView.setImageViewBitmap ( R.id.notification_favorite_button, ( (BitmapDrawable) getResources ( ).getDrawable ( R.drawable.ic_favorite_red_24dp ) ).getBitmap ( ) );
else
expanedView.setImageViewBitmap ( R.id.notification_favorite_button, ( (BitmapDrawable) getResources ( ).getDrawable ( R.mipmap.ic_favorite_border_black_24dp ) ).getBitmap ( ) );
setListeners ( smallView );
setListeners ( expanedView );
Bitmap albumArt = AudioExtensionMethods.getBitMap ( getBaseContext ( ), currentSong.getAlbumArtLocation ( ) );
if ( albumArt != null ) {
smallView.setImageViewBitmap ( R.id.notification_album_art, albumArt );
expanedView.setImageViewBitmap ( R.id.notification_album_art, albumArt );
}
else {
smallView.setImageViewBitmap ( R.id.notification_album_art, ( (BitmapDrawable) getResources ( ).getDrawable ( R.mipmap.unkown_album_art ) ).getBitmap ( ) );
expanedView.setImageViewBitmap ( R.id.notification_album_art, ( (BitmapDrawable) getResources ( ).getDrawable ( R.mipmap.unkown_album_art ) ).getBitmap ( ) );
}
// create an ongoing notif
NotificationCompat.Builder builder = new NotificationCompat.Builder ( this )
.setContent ( smallView )
.setSmallIcon ( R.mipmap.launcher_icon )
.setOngoing ( true );
// makes the notif dismissable when playback is paused
if ( PlayerConstants.SONG_PAUSED ) {
builder.setOngoing ( false );
}
if ( currentVersionSupportBigNotification ) {
builder.setCustomBigContentView ( expanedView );
}
Intent nIntent = new Intent ( this, MainActivity.class );
nIntent.putExtra ( "notificationIntent", true );
nIntent.addFlags ( Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP );
PendingIntent pendingIntent = PendingIntent.getActivity ( this, 0, nIntent, PendingIntent.FLAG_UPDATE_CURRENT );
builder.setContentIntent ( pendingIntent );
// send the notification
Notification build = builder.build ( );
notificationManager = (NotificationManager) getSystemService ( NOTIFICATION_SERVICE );
notificationManager.notify ( NOTIFICATION_ID, build );
// }
// catch (Exception ignored) {}
}
我不知道这里出了什么问题。任何帮助,将不胜感激。 P.S。我不愿意更新我的库,因为更新会使应用程序崩溃,而且我无法从头开始编写整个代码。
哇。有趣的是我是如何解决它的。事实证明,通知和小部件不支持主题或任何你称之为的主题。我在通知的布局中使用 "attr/primaryColor",将它们更改为正常颜色格式解决了问题。