音乐播放器使用nofication显示通知实例
时间: 2023-09-07 10:12:18 浏览: 44
好的,下面是一个使用Notification显示音乐播放器通知的示例代码。请注意,以下代码仅供参考,您需要根据您的具体需求进行修改和调整。
```java
// 创建通知栏管理器
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// 创建通知栏构建器
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
// 设置通知栏的标题和内容
mBuilder.setContentTitle("正在播放")
.setContentText("歌曲名称")
.setTicker("正在播放歌曲名称")
.setSmallIcon(R.drawable.ic_music);
// 创建通知栏点击意图
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pendingIntent);
// 设置通知栏的大图标
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_large_music);
mBuilder.setLargeIcon(largeIcon);
// 设置通知栏的样式为进度条样式
mBuilder.setProgress(100, 50, false);
// 发送通知
mNotificationManager.notify(1, mBuilder.build());
```
上述代码中,我们首先创建了一个NotificationManager对象,用于管理通知栏的显示和取消。然后,我们创建了一个NotificationCompat.Builder对象,用于构建通知栏的内容。在Builder中,我们设置了通知栏的标题、内容、图标、点击意图等信息。
接着,我们设置了通知栏的大图标和样式,这里我们使用了进度条样式,用于显示音乐播放的进度。最后,我们调用NotificationManager的notify方法发送通知,其中第一个参数为通知的ID,用于在后续操作中对该通知进行管理。
希望这个示例能够对您有所帮助!