//网上相关内容较少,遂记录下来,备忘.

//依然以音乐播放器demo为例.

效果截图

//锤子手机上的效果

step1 准备自定义layout

常规的实现方式,并不会因为是用于notification的而在实现上有所不同.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:orientation="vertical"
android:layout_width="170dp"
android:layout_height="wrap_content">
<TextView
android:layout_marginLeft="10dp"
android:id="@+id/music_name"
android:textSize="20dp"
android:text="要怎么办"
android:maxLines="1"
android:ellipsize="end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:layout_marginLeft="10dp"
android:layout_marginTop="6dp"
android:id="@+id/singer_name"
android:textSize="16dp"
android:text="李柏凝"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/> </LinearLayout> <LinearLayout
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<ImageButton
android:id="@+id/btn_prev"
android:background="@drawable/desk_pre"
android:layout_width="40dp"
android:layout_height="40dp"/>
<ImageButton
android:layout_marginLeft="10dp"
android:id="@+id/btn_play"
android:src="@drawable/note_btn_play"
android:background="#00ffffff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageButton
android:layout_marginLeft="10dp"
android:id="@+id/btn_next"
android:background="@drawable/desk_next"
android:layout_width="40dp"
android:layout_height="40dp"/>
</LinearLayout>
</LinearLayout>

//以下内容均为service中的实现

step2 使用以上layout文件创建一个RemoteView实例

    private void initRemoteView() {
//创建一个RemoteView实例
mRemoteViews = new RemoteViews(getPackageName(), R.layout.music_notification);
mRemoteViews.setTextViewText(R.id.music_name, mMusicDatas.get(i).getName());
mRemoteViews.setTextViewText(R.id.singer_name, mMusicDatas.get(i).getSinger()); //实例化一个指向MusicService的intent
Intent intent = new Intent(this, MusicService.class);
intent.setAction(ACTION_NOTIFICATION); //设置play按钮的点击事件
intent.putExtra(BUTTON_INDEX, BUTTON_PLAY);
PendingIntent pendingIntent = PendingIntent.getService(this, 2, intent, PendingIntent.FLAG_CANCEL_CURRENT);
mRemoteViews.setOnClickPendingIntent(R.id.btn_play, pendingIntent); //设置next按钮的点击事件
intent.putExtra(BUTTON_INDEX, BUTTON_NEXT);
pendingIntent = PendingIntent.getService(this, 3, intent, PendingIntent.FLAG_CANCEL_CURRENT);
mRemoteViews.setOnClickPendingIntent(R.id.btn_next, pendingIntent); //设置prev按钮的点击事件
intent.putExtra(BUTTON_INDEX, BUTTON_PREV);
pendingIntent = PendingIntent.getService(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
mRemoteViews.setOnClickPendingIntent(R.id.btn_prev, pendingIntent);
}

step3 使用RemoteView实例创建Nitification

    private void initNotification() {

        //实例化一个Builder
mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.drawable.default_pic);
//将remoteView设置进去
mBuilder.setContent(mRemoteViews);
//获取NotificationManager实例
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

step4 重写onStartCommand()用于处理Notification中按钮的点击事件,举例如下:

 @Override
public int onStartCommand(Intent intent, int flags, int startId) { String action = intent.getAction();
String stringExtra = intent.getStringExtra(BUTTON_INDEX); //校验action
if(TextUtils.equals(action, ACTION_NOTIFICATION)) {
//校验stringExtra
if (TextUtils.equals(stringExtra, BUTTON_NEXT)) {
i = (i+1)>=mMusicDatas.size()? 0 : i+1;
mMediaPlayer.stop();
mMediaPlayer = MediaPlayer.create(MusicService.this, mMusicDatas.get(i).getSrc());
if(isPlaying) {
mMediaPlayer.start();
} //重置Notification显示的内容
mRemoteViews.setTextViewText(R.id.music_name, mMusicDatas.get(i).getName());
mRemoteViews.setTextViewText(R.id.singer_name, mMusicDatas.get(i).getSinger());
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
} else if(TextUtils.equals(stringExtra, BUTTON_PLAY)) {
//...
} else {
//...
} }
return super.onStartCommand(intent, flags, startId);
}

以上.

github地址:https://github.com/zhangbz/MusicPlayer

android:使用RemoteView自定义Notification的更多相关文章

  1. Android -- 系统和自定义Notification

    Notification是一种让你的应用程序在不使用Activity的情况下警示用户,Notification是看不见的程序组件警示用户有需要注意的事件发生的最好途径. 作为UI部分,Notifica ...

  2. Android自定义Notification并没有那么简单

    背景 最近需要实现一个自定义Notification的功能.网上找了找代码,解决方案就是通过RemoteViews来实现.但是在实现过程中遇到不少问题,网上也没有很好的文章描述这些问题,所以在这里做个 ...

  3. Android消息通知(notification)和PendingIntent传值

    通知栏的自定义布局:转:http://blog.csdn.net/vipzjyno1/article/details/25248021 拓展 实现自定义的通知栏效果: 这里要用到RemoteViews ...

  4. Android自定义控件之自定义ViewGroup实现标签云

    前言: 前面几篇讲了自定义控件绘制原理Android自定义控件之基本原理(一),自定义属性Android自定义控件之自定义属性(二),自定义组合控件Android自定义控件之自定义组合控件(三),常言 ...

  5. Android自定义控件之自定义组合控件

    前言: 前两篇介绍了自定义控件的基础原理Android自定义控件之基本原理(一).自定义属性Android自定义控件之自定义属性(二).今天重点介绍一下如何通过自定义组合控件来提高布局的复用,降低开发 ...

  6. Android新旧版本Notification

    Android新旧版本Notification 在notification.setLatestEventInfo() 过时了 以前: NotificationManager mn = (Notific ...

  7. android开发之自定义组件

    android开发之自定义组件 一:自定义组件: 我认为,自定义组件就是android给我们提供的的一个空白的可以编辑的图片,它帮助我们实现的我们想要的界面,也就是通过自定义组件我们可以把我们要登入的 ...

  8. [Android Tips] 9. framework notification layout font size

    android 4.4 framework notification layout 相关字体大小 * title: notification_title_text_size: 18dp * conte ...

  9. Android中制作自定义dialog对话框的实例

    http://www.jb51.net/article/83319.htm   这篇文章主要介绍了Android中制作自定义dialog对话框的实例分享,安卓自带的Dialog显然不够用,因而我们要继 ...

随机推荐

  1. 升级 Visual Studio 2015 CTP 5 的坑、坑、坑

    前两天,微软发布了 Visual Studio 2015 CTP 5,全称为 Visual Studio 2015 Community Technology Preview 5,意为社区技术预览版,之 ...

  2. Java多线程系列--“JUC锁”01之 框架

    本章,我们介绍锁的架构:后面的章节将会对它们逐个进行分析介绍.目录如下:01. Java多线程系列--“JUC锁”01之 框架02. Java多线程系列--“JUC锁”02之 互斥锁Reentrant ...

  3. 如何使用免费控件将Word表格中的数据导入到Excel中

    我通常使用MS Excel来存储和处理大量数据,但有时候经常会碰到一个问题—我需要的数据存储在word表格中,而不是在Excel中,这样处理起来非常麻烦,尤其是在数据比较庞大的时候, 这时我迫切地需要 ...

  4. ASP.NET网站优化(转自一位博友的文章,写的非常好)

    不修改代码就能优化ASP.NET网站性能的一些方法 阅读目录 开始 配置OutputCache 启用内容过期 解决资源文件升级问题 启用压缩 删除无用的HttpModule 其它优化选项 本文将介绍一 ...

  5. Oracle客户端简易连接报错ORA-12154,TNS-03505

    环境: 服务端:RHEL6.5 + Oracle Server 11.2.0.4 客户端:Win2003 + Oracle Client 10.2.0.1 1.问题现象 2.Troubleshooti ...

  6. 让 OpenAL 也支持 S16 Planar(辅以 FFmpeg)

    正在制作某物品,现在做到音频部分了. 原本要采用 SDL2_mixer 的,不过实验结果表明其失真非常严重,还带有大量的电噪声.不知道是不是我打开的方式不对…… 一气之下去看 OpenAL,结果吃了闭 ...

  7. jQuery-1.9.1源码分析系列(九) CSS操作

    jquery.fn.css获取当前jQuery所匹配的元素中第一个元素的属性值[$(…).css(cssName),注意这个cssName可以是数组]或给当前jQuery所匹配的每个元素设置样式值[$ ...

  8. Redis 对比 Memcached 并在 CentOS 下进行安装配置

    了解一下 Redis Redis 是一个开源.支持网络.基于内存.键值对的 Key-Value 数据库,使用 ANSI C 编写,并提供多种语言的 API ,它几乎没有上手难度,只需要几分钟我们就能完 ...

  9. 介绍一种基于gulp对seajs的模块做合并压缩的方式

    之前的项目一直采用grunt来构建,然后用requirejs做模块化,requirejs官方有提供grunt的插件来做压缩合并.现在的项目切到了gulp,模块化用起了seajs,自然而然地也想到了模块 ...

  10. Ionic Lab下载地址

    网站被墙,留下下载链接备用 Linux版本 Mac版本 Windows版本