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

//依然以音乐播放器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. 前端工程优化:javascript的优化小结

     我觉得优化javascript是一门高深的学问,在这里也只能站在前人的肩膀上,说一些我浅显的认识,更希望的是抛钻引玉,如有不对,敬请斧正. 首先,要认识到是,优化js的关键之处在于,优化它的运行速度 ...

  2. 配置putty自动登陆服务器

    putty是一款知名的SSH工具,可以用来登陆linux服务器,提供了终端.SSH是secure Shell的缩写.我之前也有一篇文章介绍这个话题:http://www.cnblogs.com/che ...

  3. 扩展KMP算法

    一 问题定义 给定母串S和子串T,定义n为母串S的长度,m为子串T的长度,suffix[i]为第i个字符开始的母串S的后缀子串,extend[i]为suffix[i]与字串T的最长公共前缀长度.求出所 ...

  4. 原创:WeZRender:微信小程序Canvas增强组件

    WeZRender是一个微信小程序Canvas增强组件,基于HTML5 Canvas类库ZRender. 使用 WXML: <canvas style="width: 375px; h ...

  5. (十三)WebGIS中工具栏的设计之命令模式

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1.背景 从这一章节开始我们将正式进入WebGIS的工具栏中相关功能的 ...

  6. WebApp上滑加载数据...

    $(window).bind("scroll", function () { if ($(document).scrollTop() + $(window).height() &g ...

  7. JConsole远程连接配置

    JConsole远程连接还是有一点坑的.这里记录一下配置过程,好记性不如烂笔头. 1.在远程机的tomcat的catalina.sh中加入配置: JAVA_OPTS="$JAVA_OPTS ...

  8. 用c#开发微信(5)自定义菜单设置工具 (在线创建)

    读目录 1 使用 2 原理 3. 错误 上次写了<用c#开发微信 (4) 基于Senparc.Weixin框架的接收事件推送处理 (源码下载)>,有园友问到如何创建菜单的问题,今天就介绍下 ...

  9. 利用SHELL脚本实现文件完整性检测程序(1.2版更新)

    一..开发背景 因时势所逼,需要对服务器的文件系统实行监控.虽然linux下有不少入侵检测和防窜改系统,但都比较麻烦,用起来也不是很称手.自己琢磨着也不需要什么多复杂的功能,写个脚本应该就可以满足基本 ...

  10. 创建javaScript对象的方法

    一.工厂模式 function person (name,age) { var p=new Object(); p.name=name; p.age=age; p.showMessage=functi ...