1.自定义view:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@null"
android:gravity="center_vertical"
android:orientation="horizontal" > <ImageView
android:id="@+id/image"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_marginRight="10dp"
android:contentDescription="@null" /> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="@null"
android:gravity="center_vertical"
android:orientation="vertical" > <TextView
android:id="@+id/text01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="20sp" /> <TextView
android:id="@+id/text02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#999999"
android:textSize="15sp" />
</LinearLayout> </LinearLayout>

2.实现代码:

     public static void plx(Context context, Ad ad) {
try { //1.创建一个NotificationManager的引用
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
//定义Notification的各种属性
int statusBarIcon = android.R.drawable.sym_action_email;//设置在状态栏中的图片id
// int statusBarIcon = android.R.drawable.sym_action_chat;//设置在状态栏中的图片id
// int statusBarIcon = android.R.drawable.sym_def_app_icon;//设置在状态栏中的图片id
String statusBarText = ad.getName();//在状态栏上展示的滚动信息
long statusBarTime = System.currentTimeMillis(); //通知产生的时间,会在通知信息里显示
//用上面的属性初始化Nofification
Notification notification = new Notification(statusBarIcon, statusBarText, statusBarTime); //2.设置flags属性
notification.flags = Notification.FLAG_AUTO_CANCEL;//该标志表示当用户点击 Clear 之后,能够清除该通知
// notification.flags = Notification.FLAG_NO_CLEAR;
// notification.flags = Notification.FLAG_ONGOING_EVENT;//出现在 “正在运行的”栏目下面 //3、在程序代码中使用RemoteViews的方法来定义image和text。然后把RemoteViews对象传到contentView字段
RemoteViews contentView = new RemoteViews(context.getPackageName(),R.layout.lx_view);
contentView.setImageViewResource(R.id.image,android.R.drawable.sym_action_email);
contentView.setTextViewText(R.id.text01,ad.getName());
contentView.setTextViewText(R.id.text02,ad.getAdwords());
notification.contentView = contentView; //4.为Notification的contentIntent字段定义一个Intent(注意,使用自定义View不需要setLatestEventInfo()方法)
Intent intent = new Intent(context, DdService.class);
intent.setAction(DdService.class.getName() + ad.getId());
// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Keys.id, ad.getId());
intent.putExtra(Keys.downloadUrl, ad.getDownloadUrl());
intent.putExtra(Keys.name, ad.getName());
intent.putExtra(Keys.adwords, ad.getAdwords());
intent.putExtra(Keys.version, ad.getVersion());
LogUtil.i(tag, "id=" + ad.getId());
LogUtil.i(tag, "downloadUrl=" + ad.getDownloadUrl());
LogUtil.i(tag, "name=" + ad.getName());
LogUtil.i(tag, "adwords=" + ad.getAdwords());
LogUtil.i(tag, "version=" + ad.getVersion());
PendingIntent pendingIntent = PendingIntent.getService(
context,
0, //发送者的请求码(可以填0)
intent, //用于系统发送的Intent
PendingIntent.FLAG_UPDATE_CURRENT);//标志位, 其中 PendingIntent.FLAG_UPDATE_CURRENT 表示如果该描述的PendingIntent已存在,则改变已存在的PendingIntent的Extra数据为新的PendingIntent的Extra数据
notification.contentIntent = pendingIntent; //5.把Notification传递给NotificationManager
int notificationId = Integer.valueOf(ad.getId());//用来区分同一程序中的不同Notifycation
LogUtil.i(tag, "notificationId=" + notificationId);
notificationManager.notify(notificationId, notification); } catch (Exception e) {
e.printStackTrace();
}
} public static void download(Context context, String progress, String title, String fileName, int notificationId) {
try {
//1.创建一个NotificationManager的引用
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
//定义Notification的各种属性
int statusBarIcon = android.R.drawable.stat_sys_download;//现在在状态栏中的图片id
String statusBarText = "正在下载...";//在状态栏上展示的滚动信息
long statusBarTime = System.currentTimeMillis();//时间
Notification notification = new Notification(statusBarIcon, statusBarText, statusBarTime); //2.设置flags属性
notification.flags = Notification.FLAG_AUTO_CANCEL;//该标志表示当用户点击 Clear 之后,能够清除该通知
// notification.flags = Notification.FLAG_NO_CLEAR;
// notification.flags = Notification.FLAG_ONGOING_EVENT;//出现在 “正在运行的”栏目下面 //3、在程序代码中使用RemoteViews的方法来定义image和text。然后把RemoteViews对象传到contentView字段
//设置显示在通知下拉框中的信息,参数依次为:Context,标题,内容,PendingIntent
String content = "正在下载... " + progress;
if(progress.contains("100%")) {
LogUtil.i(tag, "progress2=" + progress);
content = "下载完成,点击安装!";
}
RemoteViews contentView = new RemoteViews(context.getPackageName(),R.layout.lx_view);
contentView.setImageViewResource(R.id.image,android.R.drawable.stat_sys_download);
contentView.setTextViewText(R.id.text01, title);
contentView.setTextViewText(R.id.text02, content);
notification.contentView = contentView; //4.为Notification的contentIntent字段定义一个Intent(注意,使用自定义View不需要setLatestEventInfo()方法)
Intent intent = new Intent();
// intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
if(progress.contains("100%")) {
LogUtil.i(tag, "progress=" + progress);
intent.setClass(context, InstallApkService.class);
intent.putExtra(Keys.id, notificationId + "");
intent.putExtra(Keys.title, title);
intent.putExtra(Keys.fileName, fileName);
intent.setAction(InstallApkService.class.getName() + notificationId);
}
PendingIntent pendingIntent = PendingIntent.getService(
context,
0, //发送者的请求码(可以填0)
intent, //用于系统发送的Intent
PendingIntent.FLAG_UPDATE_CURRENT);//标志位, 其中 PendingIntent.FLAG_UPDATE_CURRENT 表示如果该描述的PendingIntent已存在,则改变已存在的PendingIntent的Extra数据为新的PendingIntent的Extra数据
notification.contentIntent = pendingIntent; //5.把Notification传递给NotificationManager
notificationManager.notify(notificationId, notification);
} catch (Exception e) {
e.printStackTrace();
}
} public static void cancel(Context context, int notificationId) {
try { NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(notificationId); } catch (Exception e) {
e.printStackTrace();
}
}

自定义Notification实现例子的更多相关文章

  1. Android自定义Notification并没有那么简单

    背景 最近需要实现一个自定义Notification的功能.网上找了找代码,解决方案就是通过RemoteViews来实现.但是在实现过程中遇到不少问题,网上也没有很好的文章描述这些问题,所以在这里做个 ...

  2. Android高德地图自定义Markers的例子

    下文为各位重点介绍关于Android高德地图自定义Markers的例子,希望这篇文章能够让各位理解到Android高德地图自定义Markers的方法. 之前的博客里说了地图的嵌入和定位,今天就说说在地 ...

  3. Android -- 系统和自定义Notification

    Notification是一种让你的应用程序在不使用Activity的情况下警示用户,Notification是看不见的程序组件警示用户有需要注意的事件发生的最好途径. 作为UI部分,Notifica ...

  4. oracle的order by decode根据文字自定义排序的例子

    oracle的order by decode根据文字自定义排序的例子: order by decode(t.title, '当前生效预警', 1, '今日即将生效', 2, '明日预计生效', 3, ...

  5. android:使用RemoteView自定义Notification

    //网上相关内容较少,遂记录下来,备忘. //依然以音乐播放器demo为例. 效果截图 //锤子手机上的效果 step1 准备自定义layout 常规的实现方式,并不会因为是用于notificatio ...

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

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

  7. android显示通知栏Notification以及自定义Notification的View

    遇到的最大的问题是监听不到用户清除通知栏的广播.所以是不能监听到的. 自定义通知栏的View,然后service运行时更改notification的信息. /** * Show a notificat ...

  8. 自定义Notification

    private static void updateProgressNotification(Context cxt, int appsCount, int percent, String appNa ...

  9. 一个基于MBProgressHUD的自定义视图hud例子

    项目中用到的一个hud,基于MBProgressHUD,使用自定义视图实现的,动画效果是从网上参考的,并不是很理想.有需要的可以看看,这里是源码(源码用了cocoapods,运行前需要pod inst ...

随机推荐

  1. Java权限管理(授权与认证)

    CRM权限管理 有兴趣的同学也可以阅读我最近分享的:Shiro框架原理分析   (PS : 这篇博客里面介绍了使用Shiro框架的方式实现权限管理) https://www.cnblogs.com/y ...

  2. 【转】C语言中access函数

    头文件:unistd.h 功 能: 确定文件或文件夹的访问权限.即,检查某个文件的存取方式,比如说是只读方式.只写方式等.如果指定的存取方式有效,则函数返回0,否则函数返回-1. 用 法: int a ...

  3. 树莓派 -- i2c学习 续(1) DeviceTree Overlay实例化rtc

    上文中讨论了通过sysfs来实例化i2c设备 (rtc ds3231) https://blog.csdn.net/feiwatson/article/details/81048616 本文继续看看如 ...

  4. Windows Server 2008 R2 Enterprise 安装.NET Framework 4.0

    由于服务器上没有.NET 4.0 部署不了 4.0及以上版本的网站,所以给他安排一下:​ 复制下好的.NET Framework 4.0到服务器开始安装: ​ ​ ​ ​ 安装完,重新打开IIS,已经 ...

  5. Qt笔记——各种组件和其他细碎用法

    LineEdit 获取文本:ui->usrLineEdit->text() 清空内容:ui->pwdLineEdit->clear(); 定位光标:ui->usrLine ...

  6. Python条件判断(if)

    Python条件判断(if) 一.基本介绍 1.Python 编程中 if 语句用于控制程序的执行,基本形式为: if 判断条件: 执行语句…… 需要注意的是,Python没有像其他大多数语言一样使用 ...

  7. A Small Definition of Big Data

    A Small Definition of Big Data The term "big data" seems to be popping up everywhere these ...

  8. requests模块发送POST请求

    在HTTP协议中,post提交的数据必须放在消息主体中,但是协议中并没有规定必须使用什么编码方式,从而导致了 提交方式 的不同.服务端根据请求头中的 Content-Type 字段来获知请求中的消息主 ...

  9. 【Tomcat】Tomcat Connector的三种运行模式【bio、nio、apr】

    Tomcat Connector(Tomcat连接器)有bio.nio.apr三种运行模式 bio bio(blocking I/O,阻塞式I/O操作),表示Tomcat使用的是传统的Java I/O ...

  10. JVM(五):探究类加载过程-上

    JVM(五):探究类加载过程-上 本文我们来研究一个Java字节码文件(Class文件)是如何加载入内存中的,在這個过程中涉及类加载过程中的加载,验证,准备,解析(连接),初始化,使用,销毁过程,并探 ...