上一篇小案例,完成了一个普通的通知,点击通知启动了一个活动。但是那里的通知没有加入些“靓点”,这一篇就给它加入自定义的布局,完成自定义的通知。

应用:比如QQ音乐为例,当点击音乐播放的时候,手机屏幕上方就会展示播放音乐的通知,这个通知不仅仅拥有布局,而且响应点击事件,能完成上一曲下一曲的切换。今天这个小案例,就以此为背景展开。

首先,主活动布局不需要改变,还是放置两个按钮用于开启、关闭服务。

主活动中的代码做了了较大改变,如下:

package com.example.notification;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RemoteViews; public class MainActivity extends Activity { private Button btShow;
private Button btCancel;
private NotificationManager manager; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btShow = (Button) findViewById(R.id.show);
btCancel = (Button) findViewById(R.id.cancel); btShow.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// 展示通知
showNotificationNewAPI();
}
}); btCancel.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// 关闭通知
cancelNotification();
}
});
} /**新API展示通知*/
public void showNotificationNewAPI(){
/**获取通知对象*/
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //自定义布局的通知
Notification notification = showCustomViewNotification(); //开启通知,第一个参数类似代表该通知的id,标记为1
manager.notify(1, notification);
} /**自定义布局的通知
* @return */
private Notification showCustomViewNotification() {
NotificationCompat.Builder builder = new Builder(getApplicationContext()); builder.setSmallIcon(R.drawable.notification_music_playing)//设置通知显示的图标
.setTicker("ticker")//设置通知刚刚展示时候瞬间展示的通知信息
.setWhen(System.currentTimeMillis())//设置通知何时出现,System.currentTimeMillis()表示当前时间显示
/**以上三种方式必须设置,少一项都不发展示通知*/
.setOngoing(true)//设置滑动通知不可删除
.setContent(getRemoteView());//给通知设置内容,这个内容为自定义的布局 return builder.build();
} /**获取自定义通知使用的布局View*/
private RemoteViews getRemoteView(){
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification_layout); //设置标题
remoteViews.setTextViewText(R.id.tv_title, "爱你一万年");
//设置歌手名
remoteViews.setTextViewText(R.id.tv_arties, "刘德华"); //设置点击事件
remoteViews.setOnClickPendingIntent(R.id.ll_root, getPaddingIntent());
remoteViews.setOnClickPendingIntent(R.id.ivnext, getNextPaddingIntent());
remoteViews.setOnClickPendingIntent(R.id.ivpre, getPrePaddingIntent()); return remoteViews;
} /**获取PendingIntent*/
private PendingIntent getPaddingIntent() {
//真正的意图
Intent intent = new Intent(this, DemoActivity.class);
intent.putExtra("msg", "我是从通知栏启动的!");
// 延迟意图,用于启动活动、服务、发送广播等。携带真正的意图对象
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
return pendingIntent;
} /**获取下一曲的Paddingintent*/
private PendingIntent getNextPaddingIntent() {
// 真正的意图
Intent intent = new Intent(this, DemoActivity.class);
intent.putExtra("msg", "我是从通知栏启动的!点击了下一曲按钮");
// 延迟意图,用于启动活动、服务、发送广播等。携带真正的意图对象
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
return pendingIntent;
} /**获取上一曲的paddingintent*/
private PendingIntent getPrePaddingIntent() {
// 真正的意图
Intent intent = new Intent(this, DemoActivity.class);
intent.putExtra("msg", "我是从通知栏启动的!点击了上一曲按钮");
// 延迟意图,用于启动活动、服务、发送广播等。携带真正的意图对象
PendingIntent pendingIntent = PendingIntent.getActivity(this, 2,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
return pendingIntent;
} /**取消通知*/
public void cancelNotification(){
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//1表示我要取消标记的通知
manager.cancel(1);
}
}

代码已经写得很想详细了。。。很显然主要是下面这个方法为核心:

private RemoteViews getRemoteView(){
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification_layout); //设置标题
remoteViews.setTextViewText(R.id.tv_title, "爱你一万年");
//设置歌手名
remoteViews.setTextViewText(R.id.tv_arties, "刘德华"); //设置点击事件
remoteViews.setOnClickPendingIntent(R.id.ll_root, getPaddingIntent());
remoteViews.setOnClickPendingIntent(R.id.ivnext, getNextPaddingIntent());
remoteViews.setOnClickPendingIntent(R.id.ivpre, getPrePaddingIntent()); return remoteViews;
}

它让我们自定义布局,并且对布局中的数据做了数据展示与事件处理。相信它的意思很简单的就能体会。

我们把自定义的布局也要整理出来:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:gravity="center_vertical"
android:padding="8dp"
android:id="@+id/ll_root"
android:layout_height="wrap_content"
android:orientation="horizontal" > <!-- icon -->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/notification_music_playing"/> <!-- 歌曲信息 -->
<LinearLayout
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- 歌曲名 -->
<TextView
android:id="@+id/tv_title"
android:singleLine="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="歌曲名"
android:textColor="@color/white"/>
<!-- 歌手名 -->
<TextView
android:id="@+id/tv_arties"
android:singleLine="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="歌手名"
android:textColor="@color/halfwhite"/>
</LinearLayout>
<!-- 上一曲 -->
<ImageView
android:id="@+id/ivpre"
android:layout_marginRight="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon_notification_pre"/>
<!-- 下一曲 -->
<ImageView
android:id="@+id/ivnext"
android:layout_marginRight="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon_notification_next"/>
</LinearLayout>

还差一点,我们给通知上边的按钮添加了点击事件,事件目的是启动另一个Activity,所以要到这个activity获取这些数据,做一个土司处理。

另一个活动中的代码:

package com.example.notification;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast; public class DemoActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo);
String content = getIntent().getStringExtra("msg");
Toast.makeText(getApplicationContext(), content, 0).show();
}
}

事件结果就是打印显示一行土司。

运行看看自定义通知吧!效果图如下:

Android简易实战教程--第三十八话《自定义通知NotifiCation》的更多相关文章

  1. Android简易实战教程--第四十八话《Android - Timer、TimerTask和Handler实现倒计时》

    之前本专栏文章中的小案例有写到:第三十九话<Chronometer实现倒计时> 以及使用异步实现倒计时:第三十三话< AsyncTask异步倒计时> 本篇文章 结合Timer. ...

  2. Android简易实战教程--第三十一话《自定义土司》

    最近有点忙,好几天不更新博客了.今天就简单点,完成自定义土司. 主布局文件代码: <RelativeLayout xmlns:android="http://schemas.andro ...

  3. Android简易实战教程--第三十六话《电话录音》

    今天完成一个简单的电话录音功能,即接通电话后,立即录下自己打电话的声音.实现起来比较简单:一个服务,一个TelephonyManager.一个MediaRecorder就够了. 1.布局提供一个开启录 ...

  4. Android简易实战教程--第三十五话《音乐播放》

    已经好几天不更新博客了,今天轻松一点模拟个简单的"音乐播放器".1分钟看完~ 整个简单布局,加几个控制按钮: <LinearLayout xmlns:android=&quo ...

  5. Android简易实战教程--第三十九话《Chronometer实现倒计时》

    Android提供了实现按照秒计时的API,今天就是用这个API实现简单的倒计时. 来个布局: <?xml version="1.0" encoding="utf- ...

  6. Android简易实战教程--第三十九话《简单的模糊查询》

    今天这一篇小案例模拟模糊查询,即输入一个字符,显示手机对应的所有存在该字符的路径. 布局: <?xml version="1.0" encoding="utf-8& ...

  7. Android简易实战教程--第三十四话《 自定义SeekBar以及里面的一些小知识》

    转载本专栏文章,请注明出处尊重原创:博客地址http://blog.csdn.net/qq_32059827/article/details/52849676:小杨的博客 许多应用可能需要加入进度,例 ...

  8. Android简易实战教程--第四十六话《RecyclerView竖向和横向滚动》

    Android5.X后,引入了RecyclerView,这个控件使用起来非常的方便,不但可以完成listView的效果,而且还可以实现ListView无法实现的效果.当然,在新能方便也做了大大的提高. ...

  9. Android简易实战教程--第三十二话《使用Lrucache和NetworkImageView加载图片》

    转载本专栏每一篇博客请注明转载出处地址,尊重原创.此博客转载链接地址:小杨的博客    http://blog.csdn.net/qq_32059827/article/details/5279131 ...

随机推荐

  1. building a new horizon

    昨天是4月14日,也是我的23岁生日.正好去参加GDG举办的WTM,这次的主题是building a new horizon. 写一下印象深刻的分享者和她们的闪光点. 1.羡辙-从灵感到落地 羡辙是在 ...

  2. SQL基础-----DML语句

    之前已经介绍过SQL基础之DDL(数据库定义语言)语句,http://www.cnblogs.com/cxq0017/p/6433938.html(这是地址) 这篇文章主要介绍DML语句(数据库操纵语 ...

  3. 更换yum源

    1. 首先备份 sudo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup 2. 使用阿里云的 ...

  4. [MongoDB教程] 1.简介

    MongoDB (名称来自「humongous (巨大无比的)」), 是一个可扩展的高性能,开源,模式自由,面向文档的NoSQL,基于 分布式 文件存储,由 C++ 语言编写,设计之初旨在为 WEB ...

  5. 获取IE下载历史的具体实现

    背景: 博主去年在国内某知名互联网公司做URL安全检测时写的一份草稿. 最后却没用到项目上. 当时主要想用于URL网址安全的入库以及更新,需要建立下载文件以及URL的安全属性关联. 逻辑大致是这样的: ...

  6. [AtCoder agc021D]Reversed LCS

    Description 题库链接 在改变原串 \(S\) 最多 \(K\) 个字母的前提下,使得 \(S\) 和 \(S\) 的反串的 \(LCS\) 尽量长,问最长长度. \(1\leq K\leq ...

  7. [SDOI 2017]数字表格

    Description 题库链接 记 \(f_i\) 为 \(fibonacci\) 数列的第 \(i\) 项. 求 \[\prod_{i=1}^n\prod_{j=1}^mf_{gcd(i,j)}\ ...

  8. [AHOI 2016初中组]自行车比赛

    Description 小雪非常关注自行车比赛,尤其是环滨湖自行车赛.一年一度的环滨湖自行车赛,需要选手们连续比赛数日,最终按照累计得分决出冠军.今年一共有 N 位参赛选手.每一天的比赛总会决出当日的 ...

  9. [HNOI 2002]跳蚤

    Description Z城市居住着很多只跳蚤.在Z城市周六生活频道有一个娱乐节目.一只跳蚤将被请上一个高空钢丝的正中央.钢丝很长,可以看作是无限长.节目主持人会给该跳蚤发一张卡片.卡片上写有N+1个 ...

  10. 【ZOJ 3609】Modular Inverse 最小乘法逆元

    The modular modular multiplicative inverse of an integer a modulo m is an integer x such that a-1≡x  ...