通知栏notification是Android中一个很重要的组件,可以在顶部状态栏中存在,用户也可以通过此来操作应用,在Android中只有3.0以上的版本才加入了notification的按钮点击功能。

先看一下仿虾米写出来的通知的效果

这是一个自定义的notification,添加了,前一曲、播放、暂停、下一曲等功能,自定义的notification需要自己写布局文件,并通过remoteview显示在界面中。

res/layout/customnotice.xml

写了布局之后就需要实例化通知了

1.定义一个NotificationManager对象,并实例化

2.定义一个RemoteViews对象

3.设置RemoteViews属性

private NotificationManager manager;

private RemoteViews remoteViews;

manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

/**

* 设置通知

*/

@SuppressLint("NewApi")

private void setNotification() {

NotificationCompat.Builder builder = new Builder(this);

Intent intent = new Intent(this, MainActivity.class);

// 点击跳转到主界面

PendingIntent intent_go = PendingIntent.getActivity(this, 5, intent,

PendingIntent.FLAG_UPDATE_CURRENT);

remoteViews.setOnClickPendingIntent(R.id.notice, intent_go);

// 4个参数context, requestCode, intent, flags

PendingIntent intent_close = PendingIntent.getActivity(this, 0, intent,

PendingIntent.FLAG_UPDATE_CURRENT);

remoteViews.setOnClickPendingIntent(R.id.widget_close, intent_close);

// 设置上一曲

Intent prv = new Intent();

prv.setAction(Constants.ACTION_PRV);

PendingIntent intent_prev = PendingIntent.getBroadcast(this, 1, prv,

PendingIntent.FLAG_UPDATE_CURRENT);

remoteViews.setOnClickPendingIntent(R.id.widget_prev, intent_prev);

// 设置播放

if (Myapp.isPlay) {

Intent playorpause = new Intent();

playorpause.setAction(Constants.ACTION_PAUSE);

PendingIntent intent_play = PendingIntent.getBroadcast(this, 2,

playorpause, PendingIntent.FLAG_UPDATE_CURRENT);

remoteViews.setOnClickPendingIntent(R.id.widget_play, intent_play);

}

if (!Myapp.isPlay) {

Intent playorpause = new Intent();

playorpause.setAction(Constants.ACTION_PLAY);

PendingIntent intent_play = PendingIntent.getBroadcast(this, 6,

playorpause, PendingIntent.FLAG_UPDATE_CURRENT);

remoteViews.setOnClickPendingIntent(R.id.widget_play, intent_play);

}

// 下一曲

Intent next = new Intent();

next.setAction(Constants.ACTION_NEXT);

PendingIntent intent_next = PendingIntent.getBroadcast(this, 3, next,

PendingIntent.FLAG_UPDATE_CURRENT);

remoteViews.setOnClickPendingIntent(R.id.widget_next, intent_next);

// 设置收藏

PendingIntent intent_fav = PendingIntent.getBroadcast(this, 4, intent,

PendingIntent.FLAG_UPDATE_CURRENT);

remoteViews.setOnClickPendingIntent(R.id.widget_fav, intent_fav);

builder.setSmallIcon(R.drawable.notification_bar_icon); // 设置顶部图标

Notification notify = builder.build();

notify.contentView = remoteViews; // 设置下拉图标

notify.bigContentView = remoteViews; // 防止显示不完全,需要添加apisupport

notify.flags = Notification.FLAG_ONGOING_EVENT;

notify.icon = R.drawable.notification_bar_icon;

manager.notify(100, notify);

}

PendingIntent是一种特殊的intent,设置之后并不会马上使用,而是在真正点击后只会调用。Android默认使用的通知高度大概是64dp,无法满足大界面的显示,所以需要在其中设置bigContentView,这样才能显示大图。manager.notify(id, notification)中第一个参数是一个标识码,在取消通知的时候需要传入这个参数,第二个就是一个notify对象。

通知栏一次实例化后里面按钮的点击事件就确定了,如果当前设置的是播放的按钮,那么暂停是不会起作用的,所以需要重新设置通知,目前采用的是用Handler来更新通知栏。

public Handler handler = new Handler() {

public void handleMessage(android.os.Message msg) {

Mp3Info info = (Mp3Info) msg.obj;

Bitmap bitmap = MediaUtil.getArtwork(getApplicationContext(),

info.getId(), info.getAlbumId(), true, false);

btm_album.setImageBitmap(bitmap);

btm_artist.setText(info.getArtist());

btm_title.setText(info.getTitle());

// 播放歌曲

btm_state

.setImageResource(R.drawable.player_btn_radio_pause_normal);

// 设置通知栏的图片文字

remoteViews = new RemoteViews(getPackageName(),

R.layout.customnotice);

remoteViews.setImageViewBitmap(R.id.widget_album, bitmap);

remoteViews.setTextViewText(R.id.widget_title, info.getTitle());

remoteViews.setTextViewText(R.id.widget_artist, info.getArtist());

if (Myapp.isPlay) {

remoteViews.setImageViewResource(R.id.widget_play, R.drawable.widget_btn_pause_normal);

}else {

remoteViews.setImageViewResource(R.id.widget_play, R.drawable.widget_btn_play_normal);

}

setNotification();

};

};

在退出的时候需要取消通知栏,用NotificationManager的manager对象取消通知view,其中的100是notify设置的id,用来区别通知,因为在其他应用中可能会产生好多通知。

@Override

protected void onDestroy() {

Log.i("---"+TAG, "ondestory");

super.onDestroy();

if (remoteViews != null) {

manager.cancel(100);

}

if (receiver != null) {

unregisterReceiver(receiver);

}

}

android 音乐播放器的状态栏通知,Android仿虾米音乐播放器之通知栏notification解析...相关推荐

  1. Android音乐播放器word文档,Android音乐播放器

    Android音乐播放器 一个很简单很简单的音乐播放器 需要在res目录下新建raw文件夹,音乐文件放在里面,格式为后缀为.mp3格式的音乐 这里我们定义的音乐文件名称为big.mp3 layout布 ...

  2. android音乐播放器课程设计报告,android音乐播放器课程设计报告11.doc

    最新精品文档,知识共享! android音乐播放器课程设计报告 基于Android音乐播放器的设计与实现 滨江学院 <移动通信程序设计> 课程设计 题 目 院 系 专 业学生姓名 学 号 ...

  3. 几款最主流的音乐播放器应用源码-android源码

    影音播放 优雅音乐播放器应用源码 这是一款不错的音乐播放器应用源码案例,优雅音乐播放器应用源码,该应用的界面有点类似小米音乐播放器,应用的整天布局还没有那 人气:3328运行环境:/android/i ...

  4. Android音乐播放器eclipse,简单的Android音乐播放器 eclipse开发的基于Android平台的音乐播放器 - 下载 - 搜珍网...

    压缩包 : 音乐播放器.zip 列表 音乐播放器/ 音乐播放器/.classpath 音乐播放器/.project 音乐播放器/.settings/ 音乐播放器/.settings/org.eclip ...

  5. android学习笔记---52_发送状态栏通知

    2013/5/12 52_发送状态栏通知 ---------------------- Android的状态栏通知(Notification) 通知用于在状态栏显示消息,消息到来时以图标方式表示,如下 ...

  6. android音视频播放器开发百度云,Android 播放端 SDK

    1 概述 PLDroidPlayer 是一个适用于 Android 平台的音视频播放器 SDK,可高度定制化和二次开发,为 Android 开发者提供了简单.快捷的接口,帮助开发者在 Android ...

  7. android显示动画一直播放器,播放器适配经验总结——Android

    Android的流媒体协议支持不太好,标准只支持RTSP和MP4overHTTP.因为MP4头部大,启动会比较慢,另外MP4也不适合做直播.当然基于Android的开放性,完全可以自己做一个播放器,难 ...

  8. 安卓Service实现通知栏音乐播放器,切换歌曲,类似QQ音乐

    引言: 这样的一个音乐播放器,用到了安卓四大组件的其中三个,等于说是一个比较综合性的小功能.实现方法其实有很多,我这里给出自己的方法,不喜勿喷. 需求分析 1.音乐播放器,那我们需要一个帮助类,来构建 ...

  9. java播放器使用教程_[Java教程]Java音乐播放器

    [Java教程]Java音乐播放器 0 2016-01-07 12:00:09 乐乐音乐目前是基于musique开发的一个java音乐播放器,之前开发了一个android版本的音乐播放器,现在把and ...

最新文章

  1. seaborn使用violinplot函数可视化小提琴图、并在violinplot函数中设置inner参数来添加横线(inner=“stick“)显示数据的稠密程度
  2. 手把手教你实现基于LSTM的情感分析(LSTM-based Sentiment) Classification
  3. SAP Customer Data Cloud支持的Social Media channel
  4. decimal转为string sql_SQL注入详解|OWASP Top 10安全风险实践(二)
  5. 遍历删除_面试难题:List 如何一边遍历,一边删除?
  6. IT 行业的创新 - 创新的迷思 (1-4)
  7. python-code-11
  8. python barh_Python matplotlib.axes.Axes.barh()用法及代码示例
  9. 字符、字符集和字符编码详解(一文扫清疑惑)
  10. 本页不但包含安全的内容,也包含不安全的内容
  11. [科研论文]基于W7100的以太网读卡器的设计与实现
  12. k1658停运到什么时候_2020年春节快递几号停运 2020年春节快递停运时间电商春节放假通知...
  13. js实现添加删除表格
  14. XPI 文件安装方法
  15. 计算机桌面整洁,想让你的桌面变得整洁干净,这几款桌面整理软件别错过
  16. 如何在word中的图片上画圈标注_怎么在word图片上画圈
  17. 学习ELMo从文本中提取特征的分步NLP指南
  18. 我的第一篇文章——stm32的ADC+DMA+滤波算法
  19. php 可视化模板编辑,MetInfo
  20. matlab用正弦做随机信号程序,(MATLAB辅助现代工程数字信号处理)第6章平稳随机信号处理与分析.ppt...

热门文章

  1. 011——数组(十一)array_merge array_merge_recursive array_change_key_case
  2. FPL 2017最佳论文:如何对FPGA云发动DoS攻击?
  3. 基于 axios 的 Vue 项目 http 请求优化
  4. RTMP协议中文翻译(首发)(转)
  5. angularAMD快速入门
  6. FIFO分枝_限界算法
  7. 15个实用的grep示例
  8. perl中的map和grep
  9. 《Ext详解与实践》节选:自定义单元格的显示格式
  10. pycharm中无法import已经安装的ros中的库