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

应用:比如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. C++ 类模板与模板类详解

    在C++的Template中很多地方都用到了typename与class这两个关键字,有时候这两者可以替换,那么这两个关键字是否完全一样呢? 事实上class用于定义类,在模板引入c++后,最初定义模 ...

  2. 【贪心】Codeforces 349B.Color the Fence题解

    题目链接:http://codeforces.com/problemset/problem/349/B 题目大意 小明要从9个数字(1,2,--,9)去除一些数字拼接成一个数字,是的这个数字最大. 但 ...

  3. 学习React系列(十)——Render Props

    解决问题:将行为封装,供多个组件使用(在多个组件之间分享某段代码) 组件中的props属性中包含一个"render"属性(该属性为一个返回值为元素的方法),然后在该组件的rende ...

  4. python字符串-内置方法用法分析

    1.字母大小写相关(中文无效) 1.1 S.upper() -> string 返回一个字母全部大写的副本

  5. Centos常用命令之:文件与目录管理

    在centos中常用的文件与目录操作命令有: ◇chmod:修改文件或目录的权限 ◇mkdir:新建目录◇rmdir:删除目录◇rm:删除目录或文件◇cp:复制目录或文件◇mv:移动目录或文件 下面就 ...

  6. 利用 Scrapy 爬取知乎用户信息

    思路:通过获取知乎某个大V的关注列表和被关注列表,查看该大V和其关注用户和被关注用户的详细信息,然后通过层层递归调用,实现获取关注用户和被关注用户的关注列表和被关注列表,最终实现获取大量用户信息. 一 ...

  7. ●BZOJ 3551 [ONTAK2010]Peaks(在线)

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3551 题解: 最小生成树 Kruskal,主席树,在线 这个做法挺巧妙的...以Kruska ...

  8. [bzoj4881][Lydsy2017年5月月赛]线段游戏

    来自FallDream的博客,未经允许,请勿转载,谢谢. quailty和tangjz正在玩一个关于线段的游戏.在平面上有n条线段,编号依次为1到n.其中第i条线段的两端点坐标分别为(0,i)和(1, ...

  9. day4 liaoxuefeng--调试、线程、正则表达式

    一.错误.调试和测试 二.进程和线程 三.正则表达式

  10. c++读写MySQL

    看过很多C或是C++操作MySQL数据库的文章,大部分太吃力了,甚至有一部分根本没有很好的组织文字,初学者比较难以接受,即使是C++或是C高手也是比较难看懂.写这篇文章的目的不是别的,就一个,告诉您用 ...