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. http返回状态码错误

    415 数据格式不正确 415 Unsupported Media Type 服务器无法处理请求附带的媒体格式 后台用json接收 1.将表单数据转换成json数据 2.设置contentType:& ...

  2. C++中const与constexpr区别

    对于对象来说 const指的是编译期常量和运行时常量,两者并没有区分 constexpr特指编译期常量 对于函数来说 const可以修饰类的成员函数,被修饰的函数在执行期间不会改变对象的值. clas ...

  3. Redis Hashes 巧用sort排序

    假设我们有如下的数据结构: 我们想按download排序,并且返回hash中的其他field,需要怎么处理呢? 我们首先会想到sort命令.对,就是这个sort. 我们先看一下sort的语法: 可以看 ...

  4. 97-2016年11月1日AUDUSD在公布利率后反手做单感悟(2016.11.2)

    2016年11月1日AUDUSD在公布利率后反手做单感悟         11月1日,澳联储公布利率决议,保持利率不变,AUDUSD大涨.我在上面做空认为市场会回调.做空位置是根据多种斐波那契技术找的 ...

  5. parse XML & JSON & js

    parse XML & JSON & js how to parse xml data into json in js? https://stackoverflow.com/quest ...

  6. android开发里跳过的坑——调用已安装视频播放器在有些机器上无效

    调用已安装视频播放器播放未修改之前的代码 private void startPlay(String fileName){ File file = new File(fileName); Intent ...

  7. Thinkphp5.0 的使用模型Model查询

    Thinkphp5.0 的使用模型Model查询 一.查询多条记录 获取多个数据可以使用:select()方法和all()方法. 示例一:使用all()方法. //(1)筛选条件使用闭包函数 $res ...

  8. HashMap的工作原理以及代码实现,为什么要转换成红黑树?

    原理参考:https://blog.csdn.net/striveb/article/details/84657326 总结: 为什么当桶中键值对数量大于8才转换成红黑树,数量小于6才转换成链表? 参 ...

  9. 洛谷 P2033 Chessboard Dance

    P2033 Chessboard Dance 题目描述 在棋盘上跳舞是件有意思的事情.现在给你一张国际象棋棋盘和棋盘上的一些子以及你的初始位置和方向.求按一定操作后,棋盘的状态. 操作有四种,描述如下 ...

  10. Ubuntu 16.04安装Redis

    版本:4.0.2 下载地址:https://redis.io/download 离线版本:(链接: https://pan.baidu.com/s/1bpwDtOr 密码: 4cxk) 安装过程: 源 ...