结构图:

MainActivity.java

package com.qf.day22_notification;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.view.View;
import android.widget.RemoteViews; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} // 普通的通知
public void MyClick01(View v){ //获取通知对象
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this); //必有属性 必须要写 哪怕你不用 builder.setSmallIcon(R.drawable.image1);//图标
//其他属性
//发送通知的时间 是指显示的时间不是定时发送builder.setWhen(System.currentTimeMillis());
Bitmap bp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
builder.setLargeIcon(bp);
builder.setContentInfo("信息");
builder.setContentTitle("标题");//标题
builder.setContentText("内容hgcdakhgflasfgldsafgldguli iueroewyurhwehfldiufhlaskjfglsadfglksFGLSdjgflasdgflahgjkfdshg;hg;efgh");//内容 Intent intent = new Intent(MainActivity.this, SecondActivity.class);
/**
*
* 延迟意图 等待你的点击
* 参数1:上下文
* 参数2:请求码
* 参数3:跳转的意图
* 参数4:标记
*/
PendingIntent pendIntent = PendingIntent.getActivity
(MainActivity.this, 100, intent, PendingIntent.FLAG_ONE_SHOT); builder.setContentIntent(pendIntent); //获取通知的管理者对象
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); //使用管理者让通知展示
manager.notify(1, builder.build()); } //大视图通知 小米显示miui系统好像有问题
public void MyClick02(View v){ NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
builder.setContentText("快放假了");
builder.setContentTitle("十一");
builder.setSmallIcon(R.drawable.f020); //视图样式
NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
style.addLine("还有好多天");
style.addLine("等等就到了");
style.addLine("是啊");
style.addLine("恩");
builder.setStyle(style); NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(2, builder.build()); } //大视图通知(大图片) 小米显示miui系统好像有问题
public void MyClick03(View v){
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
builder.setContentTitle("新闻");
builder.setContentText("中彩票了");
builder.setSmallIcon(R.drawable.f020); //大图片 视图样式
NotificationCompat.BigPictureStyle style = new NotificationCompat.BigPictureStyle();
//大图片
style.bigPicture(BitmapFactory.decodeResource(getResources(), R.drawable.ccc));
style.setSummaryText("菜谱");
builder.setStyle(style); NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(3, builder.build()); } //带进度条的通知
public void MyClick04(View v){ final NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
builder.setContentTitle("昨天晚上下雨了");
builder.setContentText("早上起来 地干了");
builder.setSmallIcon(R.drawable.f020); final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //模仿下载
new Thread(){
public void run() {
//每次下载5%
for(int i=0;i<=100;i+=5){
/**
* 设置进度
* 参数1:最大的进度
* 参数2:当前进度
* 参数3:标志 进度条的样式 false :有明确进度 true:没有明确进度
*/
builder.setProgress(100, i, false);
manager.notify(4, builder.build()); try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//下载完展示
builder.setContentText("下载完成");
manager.notify(4, builder.build());
};
}.start(); } //自定义通知
public void MyClick05(View v){
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext()); builder.setContentTitle("疯狗咬人");
builder.setSmallIcon(R.drawable.ic_launcher);
//
//VIew 将xml布局转换成View对象
RemoteViews rViews = new RemoteViews(getPackageName(), R.layout.rviews_layout);
//自身的方法设置内容
rViews.setTextViewText(R.id.tv, "咬了十几个");
rViews.setImageViewResource(R.id.iv, R.drawable.f020);
builder.setContent(rViews); NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(5, builder.build()); } }

SecondActivity.java

package com.qf.day22_notification;

import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Bundle; public class SecondActivity extends Activity{ @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout); //获取通知的管理者对象
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// manager.cancel(1);//根据标识 去关闭通知
manager.cancelAll();// 关闭所有通知
} }

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" > <Button
android:onClick="MyClick01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="普通的Notification"
/>
<Button
android:onClick="MyClick02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="大视图Notification"
/>
<Button
android:onClick="MyClick03"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="大视图(大图片)Notification"
/>
<Button
android:onClick="MyClick04"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="带进度条Notification"
/>
<Button
android:onClick="MyClick05"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定义Notification"
/> </LinearLayout>

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二个Activity"
/> </LinearLayout>

rviews_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二个Activity"
/> </LinearLayout>
  • 对应样式图:

对应按钮顺序






* 有明确进度条样式 false



无明确进度条样式true 一直滑动



22 Notification 通知栏代码的更多相关文章

  1. Notification通知栏

    Notification通知栏 首先实现的功能就是通知栏显示Notification,Notification是显示在系统的通知栏上面的,所以Notification 是属于进程之前的通讯.进程之间的 ...

  2. phpcms v9最常用的22个调用代码

    新源网络工作室友情总结phpcms v9最常用的22个调用代码: 调用最新文章,带所在版块{pc:get sql="SELECT a.title, a.catid, b.catid, b.c ...

  3. android自定义Notification通知栏实例

    项目有个需求,需要在发送Notification的时候动态给定url的图片.大概思路如下:自己定义一个Notification的布局文件,这样能够很方便设置View的属性. 首先加载网络图片,使用Bi ...

  4. Notification通知栏的使用

    一.基础的知识了解 1.pendingIntent : 它是Intent的封装,可以跳转某个Activity.给Service发送一个命令.还可以发送一个广播 2.进度条的使用方法 3.更新通知栏的信 ...

  5. Android学习(二十)Notification通知栏

    一.通知栏的内容 1.图标 2.标题 3.内容 4.时间 5.点击后的相应 二.如何实现通知栏 1.获取NotificationManager. 2.显示通知栏:notify(id,notificat ...

  6. android学习笔记22——Notification

    Notification ==> Notification是显示在手机状态栏的消息,位于手机屏幕的最上方: 一般显示手机当前网络.电池状态.时间等: Notification所代表的是一种全局效 ...

  7. 安卓开发笔记——Notification通知栏

    当用户有没有接到的电话的时候,Android顶部状态栏里就会出现一个小图标.提示用户有没有处理的快讯,当拖动状态栏时,可以查看这些快讯.Android给我们提供了NotificationManager ...

  8. Android Notification通知栏的使用,通知栏在Android8.0系统不显示的问题;

    1.正常使用通知栏: /** * 创建通知栏管理工具 */ NotificationManager notificationManager = (NotificationManager) getSys ...

  9. Notification通知代码简洁使用

    1.自定义发送 Notification 的使用 1.1 通知(消息)的创建 ---------------详细介绍篇 // 不带消息内容 NSNotification *notification1 ...

随机推荐

  1. [SDOI 2008]仪仗队

    Description 作为体育委员,C君负责这次运动会仪仗队的训练.仪仗队是由学生组成的N * N的方阵,为了保证队伍在行进中整齐划一,C君会跟在仪仗队的左后方,根据其视线所及的学生人数来判断队伍是 ...

  2. [测试题]gentree

    Description 给你一个有向连通图G,每点有个权值Di(0<Di),要求生成一棵树根为1号节点的有根树T.对于树中边E,E的代价为所有从根出发的且包含E的路径的终点权值的和.现求生成树T ...

  3. 【NOIP2009】Hankson 的趣味题

    题目描述 Hanks 博士是 BT (Bio-Tech,生物技术) 领域的知名专家,他的儿子名叫 Hankson.现在,刚刚放学回家的 Hankson 正在思考一个有趣的问题. 今天在课堂上,老师讲解 ...

  4. [HNOI2009]通往城堡之路

    题目描述 听说公主被关押在城堡里,彭大侠下定决心:不管一路上有多少坎坷,不管城堡中的看守有多少厉害,不管救了公主之后公主会不会再被抓走,不管公主是否漂亮.是否会钟情于自己,他将义无反顾地朝着城堡前进. ...

  5. NOIP 2008 双栈排序

    题目描述 Tom最近在研究一个有趣的排序问题.如图所示,通过2个栈S1和S2,Tom希望借助以下4种操作实现将输入序列升序排序. 操作a 如果输入序列不为空,将第一个元素压入栈S1 操作b 如果栈S1 ...

  6. ●洛谷P2495 [SDOI2011]消耗战

    题链: https://www.luogu.org/problemnew/show/P2495题解: 虚树入门,树形dp 推荐博客:http://blog.csdn.net/lych_cys/arti ...

  7. 洛谷P2319 [HNOI2006]超级英雄

    一开始是用二分图匹配(网络流)+二分做的,后来发现直接用匈牙利更简单 #include<cstdio> #include<cstdlib> #include<algori ...

  8. CentOS7.4 源码安装MySQL8.0

    MySQL 8 正式版 8.0.11 已发布,官方表示 MySQL 8 要比 MySQL 5.7 快 2 倍,还带来了大量的改进和更快的性能! 以下为本人2018.4.23日安装过程的记录.整个过程大 ...

  9. React 深入系列4:组件的生命周期

    文:徐超,<React进阶之路>作者 授权发布,转载请注明作者及出处 React 深入系列4:组件的生命周期 React 深入系列,深入讲解了React中的重点概念.特性和模式等,旨在帮助 ...

  10. P2P技术详解(一):NAT详解——详细原理、P2P简介

    1. IPv4协议和NAT的由来 今天,无数快乐的互联网用户在尽情享受Internet带来的乐趣.他们浏览新闻,搜索资料,下载软件,广交新朋,分享信息,甚至于足不出户获取一切日用所需.企业利用互联网发 ...