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

应用:比如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. ASP.NET CORE系列【五】webapi整理以及RESTful风格化

    介绍 什么是RESTful?  这里不多做赘述,详情请百度! 哈哈,本来还想巴拉巴拉介绍一些webapi, RESTful的, 还是算了,咱们直接上干货!(原因是懒!哈哈) 使用 以前使用过mvc的人 ...

  2. Java网络编程基础(Netty预备知识)

    今天在家休息,闲来无事,写篇博客,陶冶下情操~~~ =================我是分割线================ 最近在重新学习Java网络编程基础,以便后续进行Netty的学习. 整 ...

  3. [LeetCode] Max Chunks To Make Sorted II 可排序的最大块数之二

    This question is the same as "Max Chunks to Make Sorted" except the integers of the given ...

  4. [LeetCode] Find Largest Value in Each Tree Row 找树每行最大的结点值

    You need to find the largest value in each row of a binary tree. Example: Input: 1 / \ 3 2 / \ \ 5 3 ...

  5. zabbix利用orabbix监控oracle

    Orabbix 是一个用来监控 Oracle 数据库实例的 Zabbix 插件.(插件安装在zabbix-server端) 下载地址:http://www.smartmarmot.com/produc ...

  6. 实验吧_Guess Next Session&Once More(代码审计)

    Guess Next Session 看题目提示,是一道代码审计: <?php session_start(); if (isset ($_GET['password'])) { if ($_G ...

  7. [BJOI2006]狼抓兔子

    题目描述 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的,而且现在的兔子还比较笨,它们只有两个窝,现在你做为狼王,面对下面这样一个网格的地形: ...

  8. [HNOI2001]矩阵乘积

    题目描述 输入输出格式 输入格式: 第1行为:x y (第1行为两个正整数:x,y分别表示输出结果所在的行和列) 第2行为:m n o p(第2行给出的正整数表明A为m×n矩阵,B为n×o矩阵,C为o ...

  9. Codeforces Round #464 F. Cutlet

    Description 题面 有\(2*n\)的时间,去煎一块肉,肉有两面,你需要在特定的时间内翻转,使得每一面都恰好煎了\(n\)分钟,你有\(k\)次翻转的机会,每一次表示为一段时间 \([L_i ...

  10. SPOJ 1812 Longest Common Substring II

    A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...