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

//依然以音乐播放器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. 如何用C#代码判断一个类的类型

     var s = "";  s.GetType().IsClass; 来自为知笔记(Wiz)

  2. 【记录】EF Code First 实体关联,如何添加、修改实体?

    在使用 EF Code First 的时候,我们经常会对项目中的 Entry 进行一对多.多对多的映射配置,这时候就会产生主实体和子实体的概念,我们在添加.修改他们的时候,有时候会产生一些问题,比如添 ...

  3. 从零开始构建 Wijmo & Angular 2 小应用

    中秋之际,Angular 团队发布 Angular 2 正式版,一款不错的图表控件Wijmo当天宣布支持 . Angular 2移除和替代了 Angular 1.X 中的 directives, co ...

  4. SPRING多个占位符配置文件解析源码研究--转

    原文地址:http://www.cnphp6.com/archives/85639 Spring配置文件: <context:property-placeholder location=&quo ...

  5. HTML中的meta(转载)

    meta是用来在HTML文档中模拟HTTP协议的响应头报文.meta 标签用于网页的<head>与</head>中,meta 标签的用处很多.meta 的属性有两种:name和 ...

  6. Ubuntu下搭建Mininet环境

    Mininet环境搭建分为两个步骤,如下: 1.从GITHUB上获取mininet源码到本地的opt目录下 打开Linux终端,切换目录到根目录 cd / 切换到Linux的opt目录下 cd opt ...

  7. PHP中的魔术方法(2)

    1.__get.__set这两个方法是为在类和他们的父类中没有声明的属性而设计的__get( $property ) 当调用一个未定义的属性时访问此方法__set( $property, $value ...

  8. OpenCV2:特征匹配及其优化

    在OpenCV2简单的特征匹配中对使用OpenCV2进行特征匹配的步骤做了一个简单的介绍,其匹配出的结果是非常粗糙的,在这篇文章中对使用OpenCV2进行匹配的细化做一个简单的总结.主要包括以下几个内 ...

  9. php 简单权限管理实现

    权限管理介绍.与简单实现思想 权限管理 此函数为模块访问权限管理 实现思路为:根据传进来的权限值,进入不同的权限获取区间,然后根据模块名字判断是否有这个模块的访问权限 使用说明:在每一个模块开始之前调 ...

  10. 我对Backbone.js的一些认识

    backbone.js已经不是当前最流行的前端框架了,但是对于我而言,依然具有比较好的学习价值.虽然目前来说,react,vue等mvvm框架非常火热,但是感觉自身还不到去使用这种框架的层次.这些技术 ...